perf_event.c 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368
  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 = (*event_map)[config];
  90. return mapping == HW_OP_UNSUPPORTED ? -ENOENT : mapping;
  91. }
  92. static int
  93. armpmu_map_raw_event(u32 raw_event_mask, u64 config)
  94. {
  95. return (int)(config & raw_event_mask);
  96. }
  97. static int map_cpu_event(struct perf_event *event,
  98. const unsigned (*event_map)[PERF_COUNT_HW_MAX],
  99. const unsigned (*cache_map)
  100. [PERF_COUNT_HW_CACHE_MAX]
  101. [PERF_COUNT_HW_CACHE_OP_MAX]
  102. [PERF_COUNT_HW_CACHE_RESULT_MAX],
  103. u32 raw_event_mask)
  104. {
  105. u64 config = event->attr.config;
  106. switch (event->attr.type) {
  107. case PERF_TYPE_HARDWARE:
  108. return armpmu_map_event(event_map, config);
  109. case PERF_TYPE_HW_CACHE:
  110. return armpmu_map_cache_event(cache_map, config);
  111. case PERF_TYPE_RAW:
  112. return armpmu_map_raw_event(raw_event_mask, config);
  113. }
  114. return -ENOENT;
  115. }
  116. int
  117. armpmu_event_set_period(struct perf_event *event,
  118. struct hw_perf_event *hwc,
  119. int idx)
  120. {
  121. struct arm_pmu *armpmu = to_arm_pmu(event->pmu);
  122. s64 left = local64_read(&hwc->period_left);
  123. s64 period = hwc->sample_period;
  124. int ret = 0;
  125. if (unlikely(left <= -period)) {
  126. left = period;
  127. local64_set(&hwc->period_left, left);
  128. hwc->last_period = period;
  129. ret = 1;
  130. }
  131. if (unlikely(left <= 0)) {
  132. left += period;
  133. local64_set(&hwc->period_left, left);
  134. hwc->last_period = period;
  135. ret = 1;
  136. }
  137. if (left > (s64)armpmu->max_period)
  138. left = armpmu->max_period;
  139. local64_set(&hwc->prev_count, (u64)-left);
  140. armpmu->write_counter(idx, (u64)(-left) & 0xffffffff);
  141. perf_event_update_userpage(event);
  142. return ret;
  143. }
  144. u64
  145. armpmu_event_update(struct perf_event *event,
  146. struct hw_perf_event *hwc,
  147. int idx)
  148. {
  149. struct arm_pmu *armpmu = to_arm_pmu(event->pmu);
  150. u64 delta, prev_raw_count, new_raw_count;
  151. again:
  152. prev_raw_count = local64_read(&hwc->prev_count);
  153. new_raw_count = armpmu->read_counter(idx);
  154. if (local64_cmpxchg(&hwc->prev_count, prev_raw_count,
  155. new_raw_count) != prev_raw_count)
  156. goto again;
  157. delta = (new_raw_count - prev_raw_count) & armpmu->max_period;
  158. local64_add(delta, &event->count);
  159. local64_sub(delta, &hwc->period_left);
  160. return new_raw_count;
  161. }
  162. static void
  163. armpmu_read(struct perf_event *event)
  164. {
  165. struct hw_perf_event *hwc = &event->hw;
  166. /* Don't read disabled counters! */
  167. if (hwc->idx < 0)
  168. return;
  169. armpmu_event_update(event, hwc, hwc->idx);
  170. }
  171. static void
  172. armpmu_stop(struct perf_event *event, int flags)
  173. {
  174. struct arm_pmu *armpmu = to_arm_pmu(event->pmu);
  175. struct hw_perf_event *hwc = &event->hw;
  176. /*
  177. * ARM pmu always has to update the counter, so ignore
  178. * PERF_EF_UPDATE, see comments in armpmu_start().
  179. */
  180. if (!(hwc->state & PERF_HES_STOPPED)) {
  181. armpmu->disable(hwc, hwc->idx);
  182. barrier(); /* why? */
  183. armpmu_event_update(event, hwc, hwc->idx);
  184. hwc->state |= PERF_HES_STOPPED | PERF_HES_UPTODATE;
  185. }
  186. }
  187. static void
  188. armpmu_start(struct perf_event *event, int flags)
  189. {
  190. struct arm_pmu *armpmu = to_arm_pmu(event->pmu);
  191. struct hw_perf_event *hwc = &event->hw;
  192. /*
  193. * ARM pmu always has to reprogram the period, so ignore
  194. * PERF_EF_RELOAD, see the comment below.
  195. */
  196. if (flags & PERF_EF_RELOAD)
  197. WARN_ON_ONCE(!(hwc->state & PERF_HES_UPTODATE));
  198. hwc->state = 0;
  199. /*
  200. * Set the period again. Some counters can't be stopped, so when we
  201. * were stopped we simply disabled the IRQ source and the counter
  202. * may have been left counting. If we don't do this step then we may
  203. * get an interrupt too soon or *way* too late if the overflow has
  204. * happened since disabling.
  205. */
  206. armpmu_event_set_period(event, hwc, hwc->idx);
  207. armpmu->enable(hwc, hwc->idx);
  208. }
  209. static void
  210. armpmu_del(struct perf_event *event, int flags)
  211. {
  212. struct arm_pmu *armpmu = to_arm_pmu(event->pmu);
  213. struct pmu_hw_events *hw_events = armpmu->get_hw_events();
  214. struct hw_perf_event *hwc = &event->hw;
  215. int idx = hwc->idx;
  216. WARN_ON(idx < 0);
  217. armpmu_stop(event, PERF_EF_UPDATE);
  218. hw_events->events[idx] = NULL;
  219. clear_bit(idx, hw_events->used_mask);
  220. perf_event_update_userpage(event);
  221. }
  222. static int
  223. armpmu_add(struct perf_event *event, int flags)
  224. {
  225. struct arm_pmu *armpmu = to_arm_pmu(event->pmu);
  226. struct pmu_hw_events *hw_events = armpmu->get_hw_events();
  227. struct hw_perf_event *hwc = &event->hw;
  228. int idx;
  229. int err = 0;
  230. perf_pmu_disable(event->pmu);
  231. /* If we don't have a space for the counter then finish early. */
  232. idx = armpmu->get_event_idx(hw_events, hwc);
  233. if (idx < 0) {
  234. err = idx;
  235. goto out;
  236. }
  237. /*
  238. * If there is an event in the counter we are going to use then make
  239. * sure it is disabled.
  240. */
  241. event->hw.idx = idx;
  242. armpmu->disable(hwc, idx);
  243. hw_events->events[idx] = event;
  244. hwc->state = PERF_HES_STOPPED | PERF_HES_UPTODATE;
  245. if (flags & PERF_EF_START)
  246. armpmu_start(event, PERF_EF_RELOAD);
  247. /* Propagate our changes to the userspace mapping. */
  248. perf_event_update_userpage(event);
  249. out:
  250. perf_pmu_enable(event->pmu);
  251. return err;
  252. }
  253. static int
  254. validate_event(struct pmu_hw_events *hw_events,
  255. struct perf_event *event)
  256. {
  257. struct arm_pmu *armpmu = to_arm_pmu(event->pmu);
  258. struct hw_perf_event fake_event = event->hw;
  259. struct pmu *leader_pmu = event->group_leader->pmu;
  260. if (event->pmu != leader_pmu || event->state <= PERF_EVENT_STATE_OFF)
  261. return 1;
  262. return armpmu->get_event_idx(hw_events, &fake_event) >= 0;
  263. }
  264. static int
  265. validate_group(struct perf_event *event)
  266. {
  267. struct perf_event *sibling, *leader = event->group_leader;
  268. struct pmu_hw_events fake_pmu;
  269. DECLARE_BITMAP(fake_used_mask, ARMPMU_MAX_HWEVENTS);
  270. /*
  271. * Initialise the fake PMU. We only need to populate the
  272. * used_mask for the purposes of validation.
  273. */
  274. memset(fake_used_mask, 0, sizeof(fake_used_mask));
  275. fake_pmu.used_mask = fake_used_mask;
  276. if (!validate_event(&fake_pmu, leader))
  277. return -EINVAL;
  278. list_for_each_entry(sibling, &leader->sibling_list, group_entry) {
  279. if (!validate_event(&fake_pmu, sibling))
  280. return -EINVAL;
  281. }
  282. if (!validate_event(&fake_pmu, event))
  283. return -EINVAL;
  284. return 0;
  285. }
  286. static void
  287. armpmu_release_hardware(struct arm_pmu *armpmu)
  288. {
  289. int i, irq, irqs;
  290. struct platform_device *pmu_device = armpmu->plat_device;
  291. irqs = min(pmu_device->num_resources, num_possible_cpus());
  292. for (i = 0; i < irqs; ++i) {
  293. if (!cpumask_test_and_clear_cpu(i, &armpmu->active_irqs))
  294. continue;
  295. irq = platform_get_irq(pmu_device, i);
  296. if (irq >= 0)
  297. free_irq(irq, armpmu);
  298. }
  299. }
  300. static int
  301. armpmu_reserve_hardware(struct arm_pmu *armpmu)
  302. {
  303. int i, err, irq, irqs;
  304. struct platform_device *pmu_device = armpmu->plat_device;
  305. if (!pmu_device) {
  306. pr_err("no PMU device registered\n");
  307. return -ENODEV;
  308. }
  309. irqs = min(pmu_device->num_resources, num_possible_cpus());
  310. if (irqs < 1) {
  311. pr_err("no irqs for PMUs defined\n");
  312. return -ENODEV;
  313. }
  314. for (i = 0; i < irqs; ++i) {
  315. err = 0;
  316. irq = platform_get_irq(pmu_device, i);
  317. if (irq < 0)
  318. continue;
  319. /*
  320. * If we have a single PMU interrupt that we can't shift,
  321. * assume that we're running on a uniprocessor machine and
  322. * continue. Otherwise, continue without this interrupt.
  323. */
  324. if (irq_set_affinity(irq, cpumask_of(i)) && irqs > 1) {
  325. pr_warning("unable to set irq affinity (irq=%d, cpu=%u)\n",
  326. irq, i);
  327. continue;
  328. }
  329. err = request_irq(irq, armpmu->handle_irq,
  330. IRQF_NOBALANCING,
  331. "arm-pmu", armpmu);
  332. if (err) {
  333. pr_err("unable to request IRQ%d for ARM PMU counters\n",
  334. irq);
  335. armpmu_release_hardware(armpmu);
  336. return err;
  337. }
  338. cpumask_set_cpu(i, &armpmu->active_irqs);
  339. }
  340. return 0;
  341. }
  342. static void
  343. hw_perf_event_destroy(struct perf_event *event)
  344. {
  345. struct arm_pmu *armpmu = to_arm_pmu(event->pmu);
  346. atomic_t *active_events = &armpmu->active_events;
  347. struct mutex *pmu_reserve_mutex = &armpmu->reserve_mutex;
  348. if (atomic_dec_and_mutex_lock(active_events, pmu_reserve_mutex)) {
  349. armpmu_release_hardware(armpmu);
  350. mutex_unlock(pmu_reserve_mutex);
  351. }
  352. }
  353. static int
  354. event_requires_mode_exclusion(struct perf_event_attr *attr)
  355. {
  356. return attr->exclude_idle || attr->exclude_user ||
  357. attr->exclude_kernel || attr->exclude_hv;
  358. }
  359. static int
  360. __hw_perf_event_init(struct perf_event *event)
  361. {
  362. struct arm_pmu *armpmu = to_arm_pmu(event->pmu);
  363. struct hw_perf_event *hwc = &event->hw;
  364. int mapping, err;
  365. mapping = armpmu->map_event(event);
  366. if (mapping < 0) {
  367. pr_debug("event %x:%llx not supported\n", event->attr.type,
  368. event->attr.config);
  369. return mapping;
  370. }
  371. /*
  372. * We don't assign an index until we actually place the event onto
  373. * hardware. Use -1 to signify that we haven't decided where to put it
  374. * yet. For SMP systems, each core has it's own PMU so we can't do any
  375. * clever allocation or constraints checking at this point.
  376. */
  377. hwc->idx = -1;
  378. hwc->config_base = 0;
  379. hwc->config = 0;
  380. hwc->event_base = 0;
  381. /*
  382. * Check whether we need to exclude the counter from certain modes.
  383. */
  384. if ((!armpmu->set_event_filter ||
  385. armpmu->set_event_filter(hwc, &event->attr)) &&
  386. event_requires_mode_exclusion(&event->attr)) {
  387. pr_debug("ARM performance counters do not support mode exclusion\n");
  388. return -EPERM;
  389. }
  390. /*
  391. * Store the event encoding into the config_base field.
  392. */
  393. hwc->config_base |= (unsigned long)mapping;
  394. if (!hwc->sample_period) {
  395. /*
  396. * For non-sampling runs, limit the sample_period to half
  397. * of the counter width. That way, the new counter value
  398. * is far less likely to overtake the previous one unless
  399. * you have some serious IRQ latency issues.
  400. */
  401. hwc->sample_period = armpmu->max_period >> 1;
  402. hwc->last_period = hwc->sample_period;
  403. local64_set(&hwc->period_left, hwc->sample_period);
  404. }
  405. err = 0;
  406. if (event->group_leader != event) {
  407. err = validate_group(event);
  408. if (err)
  409. return -EINVAL;
  410. }
  411. return err;
  412. }
  413. static int armpmu_event_init(struct perf_event *event)
  414. {
  415. struct arm_pmu *armpmu = to_arm_pmu(event->pmu);
  416. int err = 0;
  417. atomic_t *active_events = &armpmu->active_events;
  418. if (armpmu->map_event(event) == -ENOENT)
  419. return -ENOENT;
  420. event->destroy = hw_perf_event_destroy;
  421. if (!atomic_inc_not_zero(active_events)) {
  422. mutex_lock(&armpmu->reserve_mutex);
  423. if (atomic_read(active_events) == 0)
  424. err = armpmu_reserve_hardware(armpmu);
  425. if (!err)
  426. atomic_inc(active_events);
  427. mutex_unlock(&armpmu->reserve_mutex);
  428. }
  429. if (err)
  430. return err;
  431. err = __hw_perf_event_init(event);
  432. if (err)
  433. hw_perf_event_destroy(event);
  434. return err;
  435. }
  436. static void armpmu_enable(struct pmu *pmu)
  437. {
  438. struct arm_pmu *armpmu = to_arm_pmu(pmu);
  439. struct pmu_hw_events *hw_events = armpmu->get_hw_events();
  440. int enabled = bitmap_weight(hw_events->used_mask, armpmu->num_events);
  441. if (enabled)
  442. armpmu->start();
  443. }
  444. static void armpmu_disable(struct pmu *pmu)
  445. {
  446. struct arm_pmu *armpmu = to_arm_pmu(pmu);
  447. armpmu->stop();
  448. }
  449. static void __init armpmu_init(struct arm_pmu *armpmu)
  450. {
  451. atomic_set(&armpmu->active_events, 0);
  452. mutex_init(&armpmu->reserve_mutex);
  453. armpmu->pmu = (struct pmu) {
  454. .pmu_enable = armpmu_enable,
  455. .pmu_disable = armpmu_disable,
  456. .event_init = armpmu_event_init,
  457. .add = armpmu_add,
  458. .del = armpmu_del,
  459. .start = armpmu_start,
  460. .stop = armpmu_stop,
  461. .read = armpmu_read,
  462. };
  463. }
  464. int __init armpmu_register(struct arm_pmu *armpmu, char *name, int type)
  465. {
  466. armpmu_init(armpmu);
  467. return perf_pmu_register(&armpmu->pmu, name, type);
  468. }
  469. /*
  470. * ARMv8 PMUv3 Performance Events handling code.
  471. * Common event types.
  472. */
  473. enum armv8_pmuv3_perf_types {
  474. /* Required events. */
  475. ARMV8_PMUV3_PERFCTR_PMNC_SW_INCR = 0x00,
  476. ARMV8_PMUV3_PERFCTR_L1_DCACHE_REFILL = 0x03,
  477. ARMV8_PMUV3_PERFCTR_L1_DCACHE_ACCESS = 0x04,
  478. ARMV8_PMUV3_PERFCTR_PC_BRANCH_MIS_PRED = 0x10,
  479. ARMV8_PMUV3_PERFCTR_CLOCK_CYCLES = 0x11,
  480. ARMV8_PMUV3_PERFCTR_PC_BRANCH_PRED = 0x12,
  481. /* At least one of the following is required. */
  482. ARMV8_PMUV3_PERFCTR_INSTR_EXECUTED = 0x08,
  483. ARMV8_PMUV3_PERFCTR_OP_SPEC = 0x1B,
  484. /* Common architectural events. */
  485. ARMV8_PMUV3_PERFCTR_MEM_READ = 0x06,
  486. ARMV8_PMUV3_PERFCTR_MEM_WRITE = 0x07,
  487. ARMV8_PMUV3_PERFCTR_EXC_TAKEN = 0x09,
  488. ARMV8_PMUV3_PERFCTR_EXC_EXECUTED = 0x0A,
  489. ARMV8_PMUV3_PERFCTR_CID_WRITE = 0x0B,
  490. ARMV8_PMUV3_PERFCTR_PC_WRITE = 0x0C,
  491. ARMV8_PMUV3_PERFCTR_PC_IMM_BRANCH = 0x0D,
  492. ARMV8_PMUV3_PERFCTR_PC_PROC_RETURN = 0x0E,
  493. ARMV8_PMUV3_PERFCTR_MEM_UNALIGNED_ACCESS = 0x0F,
  494. ARMV8_PMUV3_PERFCTR_TTBR_WRITE = 0x1C,
  495. /* Common microarchitectural events. */
  496. ARMV8_PMUV3_PERFCTR_L1_ICACHE_REFILL = 0x01,
  497. ARMV8_PMUV3_PERFCTR_ITLB_REFILL = 0x02,
  498. ARMV8_PMUV3_PERFCTR_DTLB_REFILL = 0x05,
  499. ARMV8_PMUV3_PERFCTR_MEM_ACCESS = 0x13,
  500. ARMV8_PMUV3_PERFCTR_L1_ICACHE_ACCESS = 0x14,
  501. ARMV8_PMUV3_PERFCTR_L1_DCACHE_WB = 0x15,
  502. ARMV8_PMUV3_PERFCTR_L2_CACHE_ACCESS = 0x16,
  503. ARMV8_PMUV3_PERFCTR_L2_CACHE_REFILL = 0x17,
  504. ARMV8_PMUV3_PERFCTR_L2_CACHE_WB = 0x18,
  505. ARMV8_PMUV3_PERFCTR_BUS_ACCESS = 0x19,
  506. ARMV8_PMUV3_PERFCTR_MEM_ERROR = 0x1A,
  507. ARMV8_PMUV3_PERFCTR_BUS_CYCLES = 0x1D,
  508. /*
  509. * This isn't an architected event.
  510. * We detect this event number and use the cycle counter instead.
  511. */
  512. ARMV8_PMUV3_PERFCTR_CPU_CYCLES = 0xFF,
  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_CPU_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_CPU_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 __devinit 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. tail = (struct frame_tail __user *)regs->regs[29];
  1122. while (entry->nr < PERF_MAX_STACK_DEPTH &&
  1123. tail && !((unsigned long)tail & 0xf))
  1124. tail = user_backtrace(tail, entry);
  1125. }
  1126. /*
  1127. * Gets called by walk_stackframe() for every stackframe. This will be called
  1128. * whist unwinding the stackframe and is like a subroutine return so we use
  1129. * the PC.
  1130. */
  1131. static int callchain_trace(struct stackframe *frame, void *data)
  1132. {
  1133. struct perf_callchain_entry *entry = data;
  1134. perf_callchain_store(entry, frame->pc);
  1135. return 0;
  1136. }
  1137. void perf_callchain_kernel(struct perf_callchain_entry *entry,
  1138. struct pt_regs *regs)
  1139. {
  1140. struct stackframe frame;
  1141. frame.fp = regs->regs[29];
  1142. frame.sp = regs->sp;
  1143. frame.pc = regs->pc;
  1144. walk_stackframe(&frame, callchain_trace, entry);
  1145. }