perf_event.c 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411
  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. if (event->state == PERF_EVENT_STATE_OFF && !event->attr.enable_on_exec)
  268. return 1;
  269. return armpmu->get_event_idx(hw_events, &fake_event) >= 0;
  270. }
  271. static int
  272. validate_group(struct perf_event *event)
  273. {
  274. struct perf_event *sibling, *leader = event->group_leader;
  275. struct pmu_hw_events fake_pmu;
  276. DECLARE_BITMAP(fake_used_mask, ARMPMU_MAX_HWEVENTS);
  277. /*
  278. * Initialise the fake PMU. We only need to populate the
  279. * used_mask for the purposes of validation.
  280. */
  281. memset(fake_used_mask, 0, sizeof(fake_used_mask));
  282. fake_pmu.used_mask = fake_used_mask;
  283. if (!validate_event(&fake_pmu, leader))
  284. return -EINVAL;
  285. list_for_each_entry(sibling, &leader->sibling_list, group_entry) {
  286. if (!validate_event(&fake_pmu, sibling))
  287. return -EINVAL;
  288. }
  289. if (!validate_event(&fake_pmu, event))
  290. return -EINVAL;
  291. return 0;
  292. }
  293. static void
  294. armpmu_release_hardware(struct arm_pmu *armpmu)
  295. {
  296. int i, irq, irqs;
  297. struct platform_device *pmu_device = armpmu->plat_device;
  298. irqs = min(pmu_device->num_resources, num_possible_cpus());
  299. for (i = 0; i < irqs; ++i) {
  300. if (!cpumask_test_and_clear_cpu(i, &armpmu->active_irqs))
  301. continue;
  302. irq = platform_get_irq(pmu_device, i);
  303. if (irq >= 0)
  304. free_irq(irq, armpmu);
  305. }
  306. }
  307. static int
  308. armpmu_reserve_hardware(struct arm_pmu *armpmu)
  309. {
  310. int i, err, irq, irqs;
  311. struct platform_device *pmu_device = armpmu->plat_device;
  312. if (!pmu_device) {
  313. pr_err("no PMU device registered\n");
  314. return -ENODEV;
  315. }
  316. irqs = min(pmu_device->num_resources, num_possible_cpus());
  317. if (irqs < 1) {
  318. pr_err("no irqs for PMUs defined\n");
  319. return -ENODEV;
  320. }
  321. for (i = 0; i < irqs; ++i) {
  322. err = 0;
  323. irq = platform_get_irq(pmu_device, i);
  324. if (irq < 0)
  325. continue;
  326. /*
  327. * If we have a single PMU interrupt that we can't shift,
  328. * assume that we're running on a uniprocessor machine and
  329. * continue. Otherwise, continue without this interrupt.
  330. */
  331. if (irq_set_affinity(irq, cpumask_of(i)) && irqs > 1) {
  332. pr_warning("unable to set irq affinity (irq=%d, cpu=%u)\n",
  333. irq, i);
  334. continue;
  335. }
  336. err = request_irq(irq, armpmu->handle_irq,
  337. IRQF_NOBALANCING,
  338. "arm-pmu", armpmu);
  339. if (err) {
  340. pr_err("unable to request IRQ%d for ARM PMU counters\n",
  341. irq);
  342. armpmu_release_hardware(armpmu);
  343. return err;
  344. }
  345. cpumask_set_cpu(i, &armpmu->active_irqs);
  346. }
  347. return 0;
  348. }
  349. static void
  350. hw_perf_event_destroy(struct perf_event *event)
  351. {
  352. struct arm_pmu *armpmu = to_arm_pmu(event->pmu);
  353. atomic_t *active_events = &armpmu->active_events;
  354. struct mutex *pmu_reserve_mutex = &armpmu->reserve_mutex;
  355. if (atomic_dec_and_mutex_lock(active_events, pmu_reserve_mutex)) {
  356. armpmu_release_hardware(armpmu);
  357. mutex_unlock(pmu_reserve_mutex);
  358. }
  359. }
  360. static int
  361. event_requires_mode_exclusion(struct perf_event_attr *attr)
  362. {
  363. return attr->exclude_idle || attr->exclude_user ||
  364. attr->exclude_kernel || attr->exclude_hv;
  365. }
  366. static int
  367. __hw_perf_event_init(struct perf_event *event)
  368. {
  369. struct arm_pmu *armpmu = to_arm_pmu(event->pmu);
  370. struct hw_perf_event *hwc = &event->hw;
  371. int mapping, err;
  372. mapping = armpmu->map_event(event);
  373. if (mapping < 0) {
  374. pr_debug("event %x:%llx not supported\n", event->attr.type,
  375. event->attr.config);
  376. return mapping;
  377. }
  378. /*
  379. * We don't assign an index until we actually place the event onto
  380. * hardware. Use -1 to signify that we haven't decided where to put it
  381. * yet. For SMP systems, each core has it's own PMU so we can't do any
  382. * clever allocation or constraints checking at this point.
  383. */
  384. hwc->idx = -1;
  385. hwc->config_base = 0;
  386. hwc->config = 0;
  387. hwc->event_base = 0;
  388. /*
  389. * Check whether we need to exclude the counter from certain modes.
  390. */
  391. if ((!armpmu->set_event_filter ||
  392. armpmu->set_event_filter(hwc, &event->attr)) &&
  393. event_requires_mode_exclusion(&event->attr)) {
  394. pr_debug("ARM performance counters do not support mode exclusion\n");
  395. return -EPERM;
  396. }
  397. /*
  398. * Store the event encoding into the config_base field.
  399. */
  400. hwc->config_base |= (unsigned long)mapping;
  401. if (!hwc->sample_period) {
  402. /*
  403. * For non-sampling runs, limit the sample_period to half
  404. * of the counter width. That way, the new counter value
  405. * is far less likely to overtake the previous one unless
  406. * you have some serious IRQ latency issues.
  407. */
  408. hwc->sample_period = armpmu->max_period >> 1;
  409. hwc->last_period = hwc->sample_period;
  410. local64_set(&hwc->period_left, hwc->sample_period);
  411. }
  412. err = 0;
  413. if (event->group_leader != event) {
  414. err = validate_group(event);
  415. if (err)
  416. return -EINVAL;
  417. }
  418. return err;
  419. }
  420. static int armpmu_event_init(struct perf_event *event)
  421. {
  422. struct arm_pmu *armpmu = to_arm_pmu(event->pmu);
  423. int err = 0;
  424. atomic_t *active_events = &armpmu->active_events;
  425. if (armpmu->map_event(event) == -ENOENT)
  426. return -ENOENT;
  427. event->destroy = hw_perf_event_destroy;
  428. if (!atomic_inc_not_zero(active_events)) {
  429. mutex_lock(&armpmu->reserve_mutex);
  430. if (atomic_read(active_events) == 0)
  431. err = armpmu_reserve_hardware(armpmu);
  432. if (!err)
  433. atomic_inc(active_events);
  434. mutex_unlock(&armpmu->reserve_mutex);
  435. }
  436. if (err)
  437. return err;
  438. err = __hw_perf_event_init(event);
  439. if (err)
  440. hw_perf_event_destroy(event);
  441. return err;
  442. }
  443. static void armpmu_enable(struct pmu *pmu)
  444. {
  445. struct arm_pmu *armpmu = to_arm_pmu(pmu);
  446. struct pmu_hw_events *hw_events = armpmu->get_hw_events();
  447. int enabled = bitmap_weight(hw_events->used_mask, armpmu->num_events);
  448. if (enabled)
  449. armpmu->start();
  450. }
  451. static void armpmu_disable(struct pmu *pmu)
  452. {
  453. struct arm_pmu *armpmu = to_arm_pmu(pmu);
  454. armpmu->stop();
  455. }
  456. static void __init armpmu_init(struct arm_pmu *armpmu)
  457. {
  458. atomic_set(&armpmu->active_events, 0);
  459. mutex_init(&armpmu->reserve_mutex);
  460. armpmu->pmu = (struct pmu) {
  461. .pmu_enable = armpmu_enable,
  462. .pmu_disable = armpmu_disable,
  463. .event_init = armpmu_event_init,
  464. .add = armpmu_add,
  465. .del = armpmu_del,
  466. .start = armpmu_start,
  467. .stop = armpmu_stop,
  468. .read = armpmu_read,
  469. };
  470. }
  471. int __init armpmu_register(struct arm_pmu *armpmu, char *name, int type)
  472. {
  473. armpmu_init(armpmu);
  474. return perf_pmu_register(&armpmu->pmu, name, type);
  475. }
  476. /*
  477. * ARMv8 PMUv3 Performance Events handling code.
  478. * Common event types.
  479. */
  480. enum armv8_pmuv3_perf_types {
  481. /* Required events. */
  482. ARMV8_PMUV3_PERFCTR_PMNC_SW_INCR = 0x00,
  483. ARMV8_PMUV3_PERFCTR_L1_DCACHE_REFILL = 0x03,
  484. ARMV8_PMUV3_PERFCTR_L1_DCACHE_ACCESS = 0x04,
  485. ARMV8_PMUV3_PERFCTR_PC_BRANCH_MIS_PRED = 0x10,
  486. ARMV8_PMUV3_PERFCTR_CLOCK_CYCLES = 0x11,
  487. ARMV8_PMUV3_PERFCTR_PC_BRANCH_PRED = 0x12,
  488. /* At least one of the following is required. */
  489. ARMV8_PMUV3_PERFCTR_INSTR_EXECUTED = 0x08,
  490. ARMV8_PMUV3_PERFCTR_OP_SPEC = 0x1B,
  491. /* Common architectural events. */
  492. ARMV8_PMUV3_PERFCTR_MEM_READ = 0x06,
  493. ARMV8_PMUV3_PERFCTR_MEM_WRITE = 0x07,
  494. ARMV8_PMUV3_PERFCTR_EXC_TAKEN = 0x09,
  495. ARMV8_PMUV3_PERFCTR_EXC_EXECUTED = 0x0A,
  496. ARMV8_PMUV3_PERFCTR_CID_WRITE = 0x0B,
  497. ARMV8_PMUV3_PERFCTR_PC_WRITE = 0x0C,
  498. ARMV8_PMUV3_PERFCTR_PC_IMM_BRANCH = 0x0D,
  499. ARMV8_PMUV3_PERFCTR_PC_PROC_RETURN = 0x0E,
  500. ARMV8_PMUV3_PERFCTR_MEM_UNALIGNED_ACCESS = 0x0F,
  501. ARMV8_PMUV3_PERFCTR_TTBR_WRITE = 0x1C,
  502. /* Common microarchitectural events. */
  503. ARMV8_PMUV3_PERFCTR_L1_ICACHE_REFILL = 0x01,
  504. ARMV8_PMUV3_PERFCTR_ITLB_REFILL = 0x02,
  505. ARMV8_PMUV3_PERFCTR_DTLB_REFILL = 0x05,
  506. ARMV8_PMUV3_PERFCTR_MEM_ACCESS = 0x13,
  507. ARMV8_PMUV3_PERFCTR_L1_ICACHE_ACCESS = 0x14,
  508. ARMV8_PMUV3_PERFCTR_L1_DCACHE_WB = 0x15,
  509. ARMV8_PMUV3_PERFCTR_L2_CACHE_ACCESS = 0x16,
  510. ARMV8_PMUV3_PERFCTR_L2_CACHE_REFILL = 0x17,
  511. ARMV8_PMUV3_PERFCTR_L2_CACHE_WB = 0x18,
  512. ARMV8_PMUV3_PERFCTR_BUS_ACCESS = 0x19,
  513. ARMV8_PMUV3_PERFCTR_MEM_ERROR = 0x1A,
  514. ARMV8_PMUV3_PERFCTR_BUS_CYCLES = 0x1D,
  515. };
  516. /* PMUv3 HW events mapping. */
  517. static const unsigned armv8_pmuv3_perf_map[PERF_COUNT_HW_MAX] = {
  518. [PERF_COUNT_HW_CPU_CYCLES] = ARMV8_PMUV3_PERFCTR_CLOCK_CYCLES,
  519. [PERF_COUNT_HW_INSTRUCTIONS] = ARMV8_PMUV3_PERFCTR_INSTR_EXECUTED,
  520. [PERF_COUNT_HW_CACHE_REFERENCES] = ARMV8_PMUV3_PERFCTR_L1_DCACHE_ACCESS,
  521. [PERF_COUNT_HW_CACHE_MISSES] = ARMV8_PMUV3_PERFCTR_L1_DCACHE_REFILL,
  522. [PERF_COUNT_HW_BRANCH_INSTRUCTIONS] = HW_OP_UNSUPPORTED,
  523. [PERF_COUNT_HW_BRANCH_MISSES] = ARMV8_PMUV3_PERFCTR_PC_BRANCH_MIS_PRED,
  524. [PERF_COUNT_HW_BUS_CYCLES] = HW_OP_UNSUPPORTED,
  525. [PERF_COUNT_HW_STALLED_CYCLES_FRONTEND] = HW_OP_UNSUPPORTED,
  526. [PERF_COUNT_HW_STALLED_CYCLES_BACKEND] = HW_OP_UNSUPPORTED,
  527. };
  528. static const unsigned armv8_pmuv3_perf_cache_map[PERF_COUNT_HW_CACHE_MAX]
  529. [PERF_COUNT_HW_CACHE_OP_MAX]
  530. [PERF_COUNT_HW_CACHE_RESULT_MAX] = {
  531. [C(L1D)] = {
  532. [C(OP_READ)] = {
  533. [C(RESULT_ACCESS)] = ARMV8_PMUV3_PERFCTR_L1_DCACHE_ACCESS,
  534. [C(RESULT_MISS)] = ARMV8_PMUV3_PERFCTR_L1_DCACHE_REFILL,
  535. },
  536. [C(OP_WRITE)] = {
  537. [C(RESULT_ACCESS)] = ARMV8_PMUV3_PERFCTR_L1_DCACHE_ACCESS,
  538. [C(RESULT_MISS)] = ARMV8_PMUV3_PERFCTR_L1_DCACHE_REFILL,
  539. },
  540. [C(OP_PREFETCH)] = {
  541. [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
  542. [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
  543. },
  544. },
  545. [C(L1I)] = {
  546. [C(OP_READ)] = {
  547. [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
  548. [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
  549. },
  550. [C(OP_WRITE)] = {
  551. [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
  552. [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
  553. },
  554. [C(OP_PREFETCH)] = {
  555. [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
  556. [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
  557. },
  558. },
  559. [C(LL)] = {
  560. [C(OP_READ)] = {
  561. [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
  562. [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
  563. },
  564. [C(OP_WRITE)] = {
  565. [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
  566. [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
  567. },
  568. [C(OP_PREFETCH)] = {
  569. [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
  570. [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
  571. },
  572. },
  573. [C(DTLB)] = {
  574. [C(OP_READ)] = {
  575. [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
  576. [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
  577. },
  578. [C(OP_WRITE)] = {
  579. [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
  580. [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
  581. },
  582. [C(OP_PREFETCH)] = {
  583. [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
  584. [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
  585. },
  586. },
  587. [C(ITLB)] = {
  588. [C(OP_READ)] = {
  589. [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
  590. [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
  591. },
  592. [C(OP_WRITE)] = {
  593. [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
  594. [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
  595. },
  596. [C(OP_PREFETCH)] = {
  597. [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
  598. [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
  599. },
  600. },
  601. [C(BPU)] = {
  602. [C(OP_READ)] = {
  603. [C(RESULT_ACCESS)] = ARMV8_PMUV3_PERFCTR_PC_BRANCH_PRED,
  604. [C(RESULT_MISS)] = ARMV8_PMUV3_PERFCTR_PC_BRANCH_MIS_PRED,
  605. },
  606. [C(OP_WRITE)] = {
  607. [C(RESULT_ACCESS)] = ARMV8_PMUV3_PERFCTR_PC_BRANCH_PRED,
  608. [C(RESULT_MISS)] = ARMV8_PMUV3_PERFCTR_PC_BRANCH_MIS_PRED,
  609. },
  610. [C(OP_PREFETCH)] = {
  611. [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
  612. [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
  613. },
  614. },
  615. [C(NODE)] = {
  616. [C(OP_READ)] = {
  617. [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
  618. [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
  619. },
  620. [C(OP_WRITE)] = {
  621. [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
  622. [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
  623. },
  624. [C(OP_PREFETCH)] = {
  625. [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
  626. [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
  627. },
  628. },
  629. };
  630. /*
  631. * Perf Events' indices
  632. */
  633. #define ARMV8_IDX_CYCLE_COUNTER 0
  634. #define ARMV8_IDX_COUNTER0 1
  635. #define ARMV8_IDX_COUNTER_LAST (ARMV8_IDX_CYCLE_COUNTER + cpu_pmu->num_events - 1)
  636. #define ARMV8_MAX_COUNTERS 32
  637. #define ARMV8_COUNTER_MASK (ARMV8_MAX_COUNTERS - 1)
  638. /*
  639. * ARMv8 low level PMU access
  640. */
  641. /*
  642. * Perf Event to low level counters mapping
  643. */
  644. #define ARMV8_IDX_TO_COUNTER(x) \
  645. (((x) - ARMV8_IDX_COUNTER0) & ARMV8_COUNTER_MASK)
  646. /*
  647. * Per-CPU PMCR: config reg
  648. */
  649. #define ARMV8_PMCR_E (1 << 0) /* Enable all counters */
  650. #define ARMV8_PMCR_P (1 << 1) /* Reset all counters */
  651. #define ARMV8_PMCR_C (1 << 2) /* Cycle counter reset */
  652. #define ARMV8_PMCR_D (1 << 3) /* CCNT counts every 64th cpu cycle */
  653. #define ARMV8_PMCR_X (1 << 4) /* Export to ETM */
  654. #define ARMV8_PMCR_DP (1 << 5) /* Disable CCNT if non-invasive debug*/
  655. #define ARMV8_PMCR_N_SHIFT 11 /* Number of counters supported */
  656. #define ARMV8_PMCR_N_MASK 0x1f
  657. #define ARMV8_PMCR_MASK 0x3f /* Mask for writable bits */
  658. /*
  659. * PMOVSR: counters overflow flag status reg
  660. */
  661. #define ARMV8_OVSR_MASK 0xffffffff /* Mask for writable bits */
  662. #define ARMV8_OVERFLOWED_MASK ARMV8_OVSR_MASK
  663. /*
  664. * PMXEVTYPER: Event selection reg
  665. */
  666. #define ARMV8_EVTYPE_MASK 0xc80000ff /* Mask for writable bits */
  667. #define ARMV8_EVTYPE_EVENT 0xff /* Mask for EVENT bits */
  668. /*
  669. * Event filters for PMUv3
  670. */
  671. #define ARMV8_EXCLUDE_EL1 (1 << 31)
  672. #define ARMV8_EXCLUDE_EL0 (1 << 30)
  673. #define ARMV8_INCLUDE_EL2 (1 << 27)
  674. static inline u32 armv8pmu_pmcr_read(void)
  675. {
  676. u32 val;
  677. asm volatile("mrs %0, pmcr_el0" : "=r" (val));
  678. return val;
  679. }
  680. static inline void armv8pmu_pmcr_write(u32 val)
  681. {
  682. val &= ARMV8_PMCR_MASK;
  683. isb();
  684. asm volatile("msr pmcr_el0, %0" :: "r" (val));
  685. }
  686. static inline int armv8pmu_has_overflowed(u32 pmovsr)
  687. {
  688. return pmovsr & ARMV8_OVERFLOWED_MASK;
  689. }
  690. static inline int armv8pmu_counter_valid(int idx)
  691. {
  692. return idx >= ARMV8_IDX_CYCLE_COUNTER && idx <= ARMV8_IDX_COUNTER_LAST;
  693. }
  694. static inline int armv8pmu_counter_has_overflowed(u32 pmnc, int idx)
  695. {
  696. int ret = 0;
  697. u32 counter;
  698. if (!armv8pmu_counter_valid(idx)) {
  699. pr_err("CPU%u checking wrong counter %d overflow status\n",
  700. smp_processor_id(), idx);
  701. } else {
  702. counter = ARMV8_IDX_TO_COUNTER(idx);
  703. ret = pmnc & BIT(counter);
  704. }
  705. return ret;
  706. }
  707. static inline int armv8pmu_select_counter(int idx)
  708. {
  709. u32 counter;
  710. if (!armv8pmu_counter_valid(idx)) {
  711. pr_err("CPU%u selecting wrong PMNC counter %d\n",
  712. smp_processor_id(), idx);
  713. return -EINVAL;
  714. }
  715. counter = ARMV8_IDX_TO_COUNTER(idx);
  716. asm volatile("msr pmselr_el0, %0" :: "r" (counter));
  717. isb();
  718. return idx;
  719. }
  720. static inline u32 armv8pmu_read_counter(int idx)
  721. {
  722. u32 value = 0;
  723. if (!armv8pmu_counter_valid(idx))
  724. pr_err("CPU%u reading wrong counter %d\n",
  725. smp_processor_id(), idx);
  726. else if (idx == ARMV8_IDX_CYCLE_COUNTER)
  727. asm volatile("mrs %0, pmccntr_el0" : "=r" (value));
  728. else if (armv8pmu_select_counter(idx) == idx)
  729. asm volatile("mrs %0, pmxevcntr_el0" : "=r" (value));
  730. return value;
  731. }
  732. static inline void armv8pmu_write_counter(int idx, u32 value)
  733. {
  734. if (!armv8pmu_counter_valid(idx))
  735. pr_err("CPU%u writing wrong counter %d\n",
  736. smp_processor_id(), idx);
  737. else if (idx == ARMV8_IDX_CYCLE_COUNTER)
  738. asm volatile("msr pmccntr_el0, %0" :: "r" (value));
  739. else if (armv8pmu_select_counter(idx) == idx)
  740. asm volatile("msr pmxevcntr_el0, %0" :: "r" (value));
  741. }
  742. static inline void armv8pmu_write_evtype(int idx, u32 val)
  743. {
  744. if (armv8pmu_select_counter(idx) == idx) {
  745. val &= ARMV8_EVTYPE_MASK;
  746. asm volatile("msr pmxevtyper_el0, %0" :: "r" (val));
  747. }
  748. }
  749. static inline int armv8pmu_enable_counter(int idx)
  750. {
  751. u32 counter;
  752. if (!armv8pmu_counter_valid(idx)) {
  753. pr_err("CPU%u enabling wrong PMNC counter %d\n",
  754. smp_processor_id(), idx);
  755. return -EINVAL;
  756. }
  757. counter = ARMV8_IDX_TO_COUNTER(idx);
  758. asm volatile("msr pmcntenset_el0, %0" :: "r" (BIT(counter)));
  759. return idx;
  760. }
  761. static inline int armv8pmu_disable_counter(int idx)
  762. {
  763. u32 counter;
  764. if (!armv8pmu_counter_valid(idx)) {
  765. pr_err("CPU%u disabling wrong PMNC counter %d\n",
  766. smp_processor_id(), idx);
  767. return -EINVAL;
  768. }
  769. counter = ARMV8_IDX_TO_COUNTER(idx);
  770. asm volatile("msr pmcntenclr_el0, %0" :: "r" (BIT(counter)));
  771. return idx;
  772. }
  773. static inline int armv8pmu_enable_intens(int idx)
  774. {
  775. u32 counter;
  776. if (!armv8pmu_counter_valid(idx)) {
  777. pr_err("CPU%u enabling wrong PMNC counter IRQ enable %d\n",
  778. smp_processor_id(), idx);
  779. return -EINVAL;
  780. }
  781. counter = ARMV8_IDX_TO_COUNTER(idx);
  782. asm volatile("msr pmintenset_el1, %0" :: "r" (BIT(counter)));
  783. return idx;
  784. }
  785. static inline int armv8pmu_disable_intens(int idx)
  786. {
  787. u32 counter;
  788. if (!armv8pmu_counter_valid(idx)) {
  789. pr_err("CPU%u disabling wrong PMNC counter IRQ enable %d\n",
  790. smp_processor_id(), idx);
  791. return -EINVAL;
  792. }
  793. counter = ARMV8_IDX_TO_COUNTER(idx);
  794. asm volatile("msr pmintenclr_el1, %0" :: "r" (BIT(counter)));
  795. isb();
  796. /* Clear the overflow flag in case an interrupt is pending. */
  797. asm volatile("msr pmovsclr_el0, %0" :: "r" (BIT(counter)));
  798. isb();
  799. return idx;
  800. }
  801. static inline u32 armv8pmu_getreset_flags(void)
  802. {
  803. u32 value;
  804. /* Read */
  805. asm volatile("mrs %0, pmovsclr_el0" : "=r" (value));
  806. /* Write to clear flags */
  807. value &= ARMV8_OVSR_MASK;
  808. asm volatile("msr pmovsclr_el0, %0" :: "r" (value));
  809. return value;
  810. }
  811. static void armv8pmu_enable_event(struct hw_perf_event *hwc, int idx)
  812. {
  813. unsigned long flags;
  814. struct pmu_hw_events *events = cpu_pmu->get_hw_events();
  815. /*
  816. * Enable counter and interrupt, and set the counter to count
  817. * the event that we're interested in.
  818. */
  819. raw_spin_lock_irqsave(&events->pmu_lock, flags);
  820. /*
  821. * Disable counter
  822. */
  823. armv8pmu_disable_counter(idx);
  824. /*
  825. * Set event (if destined for PMNx counters).
  826. */
  827. armv8pmu_write_evtype(idx, hwc->config_base);
  828. /*
  829. * Enable interrupt for this counter
  830. */
  831. armv8pmu_enable_intens(idx);
  832. /*
  833. * Enable counter
  834. */
  835. armv8pmu_enable_counter(idx);
  836. raw_spin_unlock_irqrestore(&events->pmu_lock, flags);
  837. }
  838. static void armv8pmu_disable_event(struct hw_perf_event *hwc, int idx)
  839. {
  840. unsigned long flags;
  841. struct pmu_hw_events *events = cpu_pmu->get_hw_events();
  842. /*
  843. * Disable counter and interrupt
  844. */
  845. raw_spin_lock_irqsave(&events->pmu_lock, flags);
  846. /*
  847. * Disable counter
  848. */
  849. armv8pmu_disable_counter(idx);
  850. /*
  851. * Disable interrupt for this counter
  852. */
  853. armv8pmu_disable_intens(idx);
  854. raw_spin_unlock_irqrestore(&events->pmu_lock, flags);
  855. }
  856. static irqreturn_t armv8pmu_handle_irq(int irq_num, void *dev)
  857. {
  858. u32 pmovsr;
  859. struct perf_sample_data data;
  860. struct pmu_hw_events *cpuc;
  861. struct pt_regs *regs;
  862. int idx;
  863. /*
  864. * Get and reset the IRQ flags
  865. */
  866. pmovsr = armv8pmu_getreset_flags();
  867. /*
  868. * Did an overflow occur?
  869. */
  870. if (!armv8pmu_has_overflowed(pmovsr))
  871. return IRQ_NONE;
  872. /*
  873. * Handle the counter(s) overflow(s)
  874. */
  875. regs = get_irq_regs();
  876. cpuc = &__get_cpu_var(cpu_hw_events);
  877. for (idx = 0; idx < cpu_pmu->num_events; ++idx) {
  878. struct perf_event *event = cpuc->events[idx];
  879. struct hw_perf_event *hwc;
  880. /* Ignore if we don't have an event. */
  881. if (!event)
  882. continue;
  883. /*
  884. * We have a single interrupt for all counters. Check that
  885. * each counter has overflowed before we process it.
  886. */
  887. if (!armv8pmu_counter_has_overflowed(pmovsr, idx))
  888. continue;
  889. hwc = &event->hw;
  890. armpmu_event_update(event, hwc, idx);
  891. perf_sample_data_init(&data, 0, hwc->last_period);
  892. if (!armpmu_event_set_period(event, hwc, idx))
  893. continue;
  894. if (perf_event_overflow(event, &data, regs))
  895. cpu_pmu->disable(hwc, idx);
  896. }
  897. /*
  898. * Handle the pending perf events.
  899. *
  900. * Note: this call *must* be run with interrupts disabled. For
  901. * platforms that can have the PMU interrupts raised as an NMI, this
  902. * will not work.
  903. */
  904. irq_work_run();
  905. return IRQ_HANDLED;
  906. }
  907. static void armv8pmu_start(void)
  908. {
  909. unsigned long flags;
  910. struct pmu_hw_events *events = cpu_pmu->get_hw_events();
  911. raw_spin_lock_irqsave(&events->pmu_lock, flags);
  912. /* Enable all counters */
  913. armv8pmu_pmcr_write(armv8pmu_pmcr_read() | ARMV8_PMCR_E);
  914. raw_spin_unlock_irqrestore(&events->pmu_lock, flags);
  915. }
  916. static void armv8pmu_stop(void)
  917. {
  918. unsigned long flags;
  919. struct pmu_hw_events *events = cpu_pmu->get_hw_events();
  920. raw_spin_lock_irqsave(&events->pmu_lock, flags);
  921. /* Disable all counters */
  922. armv8pmu_pmcr_write(armv8pmu_pmcr_read() & ~ARMV8_PMCR_E);
  923. raw_spin_unlock_irqrestore(&events->pmu_lock, flags);
  924. }
  925. static int armv8pmu_get_event_idx(struct pmu_hw_events *cpuc,
  926. struct hw_perf_event *event)
  927. {
  928. int idx;
  929. unsigned long evtype = event->config_base & ARMV8_EVTYPE_EVENT;
  930. /* Always place a cycle counter into the cycle counter. */
  931. if (evtype == ARMV8_PMUV3_PERFCTR_CLOCK_CYCLES) {
  932. if (test_and_set_bit(ARMV8_IDX_CYCLE_COUNTER, cpuc->used_mask))
  933. return -EAGAIN;
  934. return ARMV8_IDX_CYCLE_COUNTER;
  935. }
  936. /*
  937. * For anything other than a cycle counter, try and use
  938. * the events counters
  939. */
  940. for (idx = ARMV8_IDX_COUNTER0; idx < cpu_pmu->num_events; ++idx) {
  941. if (!test_and_set_bit(idx, cpuc->used_mask))
  942. return idx;
  943. }
  944. /* The counters are all in use. */
  945. return -EAGAIN;
  946. }
  947. /*
  948. * Add an event filter to a given event. This will only work for PMUv2 PMUs.
  949. */
  950. static int armv8pmu_set_event_filter(struct hw_perf_event *event,
  951. struct perf_event_attr *attr)
  952. {
  953. unsigned long config_base = 0;
  954. if (attr->exclude_idle)
  955. return -EPERM;
  956. if (attr->exclude_user)
  957. config_base |= ARMV8_EXCLUDE_EL0;
  958. if (attr->exclude_kernel)
  959. config_base |= ARMV8_EXCLUDE_EL1;
  960. if (!attr->exclude_hv)
  961. config_base |= ARMV8_INCLUDE_EL2;
  962. /*
  963. * Install the filter into config_base as this is used to
  964. * construct the event type.
  965. */
  966. event->config_base = config_base;
  967. return 0;
  968. }
  969. static void armv8pmu_reset(void *info)
  970. {
  971. u32 idx, nb_cnt = cpu_pmu->num_events;
  972. /* The counter and interrupt enable registers are unknown at reset. */
  973. for (idx = ARMV8_IDX_CYCLE_COUNTER; idx < nb_cnt; ++idx)
  974. armv8pmu_disable_event(NULL, idx);
  975. /* Initialize & Reset PMNC: C and P bits. */
  976. armv8pmu_pmcr_write(ARMV8_PMCR_P | ARMV8_PMCR_C);
  977. /* Disable access from userspace. */
  978. asm volatile("msr pmuserenr_el0, %0" :: "r" (0));
  979. }
  980. static int armv8_pmuv3_map_event(struct perf_event *event)
  981. {
  982. return map_cpu_event(event, &armv8_pmuv3_perf_map,
  983. &armv8_pmuv3_perf_cache_map, 0xFF);
  984. }
  985. static struct arm_pmu armv8pmu = {
  986. .handle_irq = armv8pmu_handle_irq,
  987. .enable = armv8pmu_enable_event,
  988. .disable = armv8pmu_disable_event,
  989. .read_counter = armv8pmu_read_counter,
  990. .write_counter = armv8pmu_write_counter,
  991. .get_event_idx = armv8pmu_get_event_idx,
  992. .start = armv8pmu_start,
  993. .stop = armv8pmu_stop,
  994. .reset = armv8pmu_reset,
  995. .max_period = (1LLU << 32) - 1,
  996. };
  997. static u32 __init armv8pmu_read_num_pmnc_events(void)
  998. {
  999. u32 nb_cnt;
  1000. /* Read the nb of CNTx counters supported from PMNC */
  1001. nb_cnt = (armv8pmu_pmcr_read() >> ARMV8_PMCR_N_SHIFT) & ARMV8_PMCR_N_MASK;
  1002. /* Add the CPU cycles counter and return */
  1003. return nb_cnt + 1;
  1004. }
  1005. static struct arm_pmu *__init armv8_pmuv3_pmu_init(void)
  1006. {
  1007. armv8pmu.name = "arm/armv8-pmuv3";
  1008. armv8pmu.map_event = armv8_pmuv3_map_event;
  1009. armv8pmu.num_events = armv8pmu_read_num_pmnc_events();
  1010. armv8pmu.set_event_filter = armv8pmu_set_event_filter;
  1011. return &armv8pmu;
  1012. }
  1013. /*
  1014. * Ensure the PMU has sane values out of reset.
  1015. * This requires SMP to be available, so exists as a separate initcall.
  1016. */
  1017. static int __init
  1018. cpu_pmu_reset(void)
  1019. {
  1020. if (cpu_pmu && cpu_pmu->reset)
  1021. return on_each_cpu(cpu_pmu->reset, NULL, 1);
  1022. return 0;
  1023. }
  1024. arch_initcall(cpu_pmu_reset);
  1025. /*
  1026. * PMU platform driver and devicetree bindings.
  1027. */
  1028. static struct of_device_id armpmu_of_device_ids[] = {
  1029. {.compatible = "arm,armv8-pmuv3"},
  1030. {},
  1031. };
  1032. static int armpmu_device_probe(struct platform_device *pdev)
  1033. {
  1034. if (!cpu_pmu)
  1035. return -ENODEV;
  1036. cpu_pmu->plat_device = pdev;
  1037. return 0;
  1038. }
  1039. static struct platform_driver armpmu_driver = {
  1040. .driver = {
  1041. .name = "arm-pmu",
  1042. .of_match_table = armpmu_of_device_ids,
  1043. },
  1044. .probe = armpmu_device_probe,
  1045. };
  1046. static int __init register_pmu_driver(void)
  1047. {
  1048. return platform_driver_register(&armpmu_driver);
  1049. }
  1050. device_initcall(register_pmu_driver);
  1051. static struct pmu_hw_events *armpmu_get_cpu_events(void)
  1052. {
  1053. return &__get_cpu_var(cpu_hw_events);
  1054. }
  1055. static void __init cpu_pmu_init(struct arm_pmu *armpmu)
  1056. {
  1057. int cpu;
  1058. for_each_possible_cpu(cpu) {
  1059. struct pmu_hw_events *events = &per_cpu(cpu_hw_events, cpu);
  1060. events->events = per_cpu(hw_events, cpu);
  1061. events->used_mask = per_cpu(used_mask, cpu);
  1062. raw_spin_lock_init(&events->pmu_lock);
  1063. }
  1064. armpmu->get_hw_events = armpmu_get_cpu_events;
  1065. }
  1066. static int __init init_hw_perf_events(void)
  1067. {
  1068. u64 dfr = read_cpuid(ID_AA64DFR0_EL1);
  1069. switch ((dfr >> 8) & 0xf) {
  1070. case 0x1: /* PMUv3 */
  1071. cpu_pmu = armv8_pmuv3_pmu_init();
  1072. break;
  1073. }
  1074. if (cpu_pmu) {
  1075. pr_info("enabled with %s PMU driver, %d counters available\n",
  1076. cpu_pmu->name, cpu_pmu->num_events);
  1077. cpu_pmu_init(cpu_pmu);
  1078. armpmu_register(cpu_pmu, "cpu", PERF_TYPE_RAW);
  1079. } else {
  1080. pr_info("no hardware support available\n");
  1081. }
  1082. return 0;
  1083. }
  1084. early_initcall(init_hw_perf_events);
  1085. /*
  1086. * Callchain handling code.
  1087. */
  1088. struct frame_tail {
  1089. struct frame_tail __user *fp;
  1090. unsigned long lr;
  1091. } __attribute__((packed));
  1092. /*
  1093. * Get the return address for a single stackframe and return a pointer to the
  1094. * next frame tail.
  1095. */
  1096. static struct frame_tail __user *
  1097. user_backtrace(struct frame_tail __user *tail,
  1098. struct perf_callchain_entry *entry)
  1099. {
  1100. struct frame_tail buftail;
  1101. unsigned long err;
  1102. /* Also check accessibility of one struct frame_tail beyond */
  1103. if (!access_ok(VERIFY_READ, tail, sizeof(buftail)))
  1104. return NULL;
  1105. pagefault_disable();
  1106. err = __copy_from_user_inatomic(&buftail, tail, sizeof(buftail));
  1107. pagefault_enable();
  1108. if (err)
  1109. return NULL;
  1110. perf_callchain_store(entry, buftail.lr);
  1111. /*
  1112. * Frame pointers should strictly progress back up the stack
  1113. * (towards higher addresses).
  1114. */
  1115. if (tail >= buftail.fp)
  1116. return NULL;
  1117. return buftail.fp;
  1118. }
  1119. void perf_callchain_user(struct perf_callchain_entry *entry,
  1120. struct pt_regs *regs)
  1121. {
  1122. struct frame_tail __user *tail;
  1123. if (perf_guest_cbs && perf_guest_cbs->is_in_guest()) {
  1124. /* We don't support guest os callchain now */
  1125. return;
  1126. }
  1127. perf_callchain_store(entry, regs->pc);
  1128. tail = (struct frame_tail __user *)regs->regs[29];
  1129. while (entry->nr < PERF_MAX_STACK_DEPTH &&
  1130. tail && !((unsigned long)tail & 0xf))
  1131. tail = user_backtrace(tail, entry);
  1132. }
  1133. /*
  1134. * Gets called by walk_stackframe() for every stackframe. This will be called
  1135. * whist unwinding the stackframe and is like a subroutine return so we use
  1136. * the PC.
  1137. */
  1138. static int callchain_trace(struct stackframe *frame, void *data)
  1139. {
  1140. struct perf_callchain_entry *entry = data;
  1141. perf_callchain_store(entry, frame->pc);
  1142. return 0;
  1143. }
  1144. void perf_callchain_kernel(struct perf_callchain_entry *entry,
  1145. struct pt_regs *regs)
  1146. {
  1147. struct stackframe frame;
  1148. if (perf_guest_cbs && perf_guest_cbs->is_in_guest()) {
  1149. /* We don't support guest os callchain now */
  1150. return;
  1151. }
  1152. frame.fp = regs->regs[29];
  1153. frame.sp = regs->sp;
  1154. frame.pc = regs->pc;
  1155. walk_stackframe(&frame, callchain_trace, entry);
  1156. }
  1157. unsigned long perf_instruction_pointer(struct pt_regs *regs)
  1158. {
  1159. if (perf_guest_cbs && perf_guest_cbs->is_in_guest())
  1160. return perf_guest_cbs->get_guest_ip();
  1161. return instruction_pointer(regs);
  1162. }
  1163. unsigned long perf_misc_flags(struct pt_regs *regs)
  1164. {
  1165. int misc = 0;
  1166. if (perf_guest_cbs && perf_guest_cbs->is_in_guest()) {
  1167. if (perf_guest_cbs->is_user_mode())
  1168. misc |= PERF_RECORD_MISC_GUEST_USER;
  1169. else
  1170. misc |= PERF_RECORD_MISC_GUEST_KERNEL;
  1171. } else {
  1172. if (user_mode(regs))
  1173. misc |= PERF_RECORD_MISC_USER;
  1174. else
  1175. misc |= PERF_RECORD_MISC_KERNEL;
  1176. }
  1177. return misc;
  1178. }