perf_event.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Linux performance counter support for LoongArch.
  4. *
  5. * Copyright (C) 2022 Loongson Technology Corporation Limited
  6. *
  7. * Derived from MIPS:
  8. * Copyright (C) 2010 MIPS Technologies, Inc.
  9. * Copyright (C) 2011 Cavium Networks, Inc.
  10. * Author: Deng-Cheng Zhu
  11. */
  12. #include <linux/cpumask.h>
  13. #include <linux/interrupt.h>
  14. #include <linux/smp.h>
  15. #include <linux/kernel.h>
  16. #include <linux/perf_event.h>
  17. #include <linux/uaccess.h>
  18. #include <linux/sched/task_stack.h>
  19. #include <asm/irq.h>
  20. #include <asm/irq_regs.h>
  21. #include <asm/stacktrace.h>
  22. #include <asm/unwind.h>
  23. /*
  24. * Get the return address for a single stackframe and return a pointer to the
  25. * next frame tail.
  26. */
  27. static unsigned long
  28. user_backtrace(struct perf_callchain_entry_ctx *entry, unsigned long fp)
  29. {
  30. unsigned long err;
  31. unsigned long __user *user_frame_tail;
  32. struct stack_frame buftail;
  33. user_frame_tail = (unsigned long __user *)(fp - sizeof(struct stack_frame));
  34. /* Also check accessibility of one struct frame_tail beyond */
  35. if (!access_ok(user_frame_tail, sizeof(buftail)))
  36. return 0;
  37. pagefault_disable();
  38. err = __copy_from_user_inatomic(&buftail, user_frame_tail, sizeof(buftail));
  39. pagefault_enable();
  40. if (err || (unsigned long)user_frame_tail >= buftail.fp)
  41. return 0;
  42. perf_callchain_store(entry, buftail.ra);
  43. return buftail.fp;
  44. }
  45. void perf_callchain_user(struct perf_callchain_entry_ctx *entry,
  46. struct pt_regs *regs)
  47. {
  48. unsigned long fp;
  49. if (perf_guest_state()) {
  50. /* We don't support guest os callchain now */
  51. return;
  52. }
  53. perf_callchain_store(entry, regs->csr_era);
  54. fp = regs->regs[22];
  55. while (entry->nr < entry->max_stack && fp && !((unsigned long)fp & 0xf))
  56. fp = user_backtrace(entry, fp);
  57. }
  58. void perf_callchain_kernel(struct perf_callchain_entry_ctx *entry,
  59. struct pt_regs *regs)
  60. {
  61. struct unwind_state state;
  62. unsigned long addr;
  63. for (unwind_start(&state, current, regs);
  64. !unwind_done(&state); unwind_next_frame(&state)) {
  65. addr = unwind_get_return_address(&state);
  66. if (!addr || perf_callchain_store(entry, addr))
  67. return;
  68. }
  69. }
  70. #define LOONGARCH_MAX_HWEVENTS 32
  71. struct cpu_hw_events {
  72. /* Array of events on this cpu. */
  73. struct perf_event *events[LOONGARCH_MAX_HWEVENTS];
  74. /*
  75. * Set the bit (indexed by the counter number) when the counter
  76. * is used for an event.
  77. */
  78. unsigned long used_mask[BITS_TO_LONGS(LOONGARCH_MAX_HWEVENTS)];
  79. /*
  80. * Software copy of the control register for each performance counter.
  81. */
  82. unsigned int saved_ctrl[LOONGARCH_MAX_HWEVENTS];
  83. };
  84. static DEFINE_PER_CPU(struct cpu_hw_events, cpu_hw_events) = {
  85. .saved_ctrl = {0},
  86. };
  87. /* The description of LoongArch performance events. */
  88. struct loongarch_perf_event {
  89. unsigned int event_id;
  90. };
  91. static struct loongarch_perf_event raw_event;
  92. static DEFINE_MUTEX(raw_event_mutex);
  93. #define C(x) PERF_COUNT_HW_CACHE_##x
  94. #define HW_OP_UNSUPPORTED 0xffffffff
  95. #define CACHE_OP_UNSUPPORTED 0xffffffff
  96. #define PERF_MAP_ALL_UNSUPPORTED \
  97. [0 ... PERF_COUNT_HW_MAX - 1] = {HW_OP_UNSUPPORTED}
  98. #define PERF_CACHE_MAP_ALL_UNSUPPORTED \
  99. [0 ... C(MAX) - 1] = { \
  100. [0 ... C(OP_MAX) - 1] = { \
  101. [0 ... C(RESULT_MAX) - 1] = {CACHE_OP_UNSUPPORTED}, \
  102. }, \
  103. }
  104. struct loongarch_pmu {
  105. u64 max_period;
  106. u64 valid_count;
  107. u64 overflow;
  108. const char *name;
  109. unsigned int num_counters;
  110. u64 (*read_counter)(unsigned int idx);
  111. void (*write_counter)(unsigned int idx, u64 val);
  112. const struct loongarch_perf_event *(*map_raw_event)(u64 config);
  113. const struct loongarch_perf_event (*general_event_map)[PERF_COUNT_HW_MAX];
  114. const struct loongarch_perf_event (*cache_event_map)
  115. [PERF_COUNT_HW_CACHE_MAX]
  116. [PERF_COUNT_HW_CACHE_OP_MAX]
  117. [PERF_COUNT_HW_CACHE_RESULT_MAX];
  118. };
  119. static struct loongarch_pmu loongarch_pmu;
  120. #define M_PERFCTL_EVENT(event) (event & CSR_PERFCTRL_EVENT)
  121. #define M_PERFCTL_COUNT_EVENT_WHENEVER (CSR_PERFCTRL_PLV0 | \
  122. CSR_PERFCTRL_PLV1 | \
  123. CSR_PERFCTRL_PLV2 | \
  124. CSR_PERFCTRL_PLV3 | \
  125. CSR_PERFCTRL_IE)
  126. #define M_PERFCTL_CONFIG_MASK 0x1f0000
  127. static void pause_local_counters(void);
  128. static void resume_local_counters(void);
  129. static u64 loongarch_pmu_read_counter(unsigned int idx)
  130. {
  131. u64 val = -1;
  132. switch (idx) {
  133. case 0:
  134. val = read_csr_perfcntr0();
  135. break;
  136. case 1:
  137. val = read_csr_perfcntr1();
  138. break;
  139. case 2:
  140. val = read_csr_perfcntr2();
  141. break;
  142. case 3:
  143. val = read_csr_perfcntr3();
  144. break;
  145. default:
  146. WARN_ONCE(1, "Invalid performance counter number (%d)\n", idx);
  147. return 0;
  148. }
  149. return val;
  150. }
  151. static void loongarch_pmu_write_counter(unsigned int idx, u64 val)
  152. {
  153. switch (idx) {
  154. case 0:
  155. write_csr_perfcntr0(val);
  156. return;
  157. case 1:
  158. write_csr_perfcntr1(val);
  159. return;
  160. case 2:
  161. write_csr_perfcntr2(val);
  162. return;
  163. case 3:
  164. write_csr_perfcntr3(val);
  165. return;
  166. default:
  167. WARN_ONCE(1, "Invalid performance counter number (%d)\n", idx);
  168. return;
  169. }
  170. }
  171. static unsigned int loongarch_pmu_read_control(unsigned int idx)
  172. {
  173. unsigned int val = -1;
  174. switch (idx) {
  175. case 0:
  176. val = read_csr_perfctrl0();
  177. break;
  178. case 1:
  179. val = read_csr_perfctrl1();
  180. break;
  181. case 2:
  182. val = read_csr_perfctrl2();
  183. break;
  184. case 3:
  185. val = read_csr_perfctrl3();
  186. break;
  187. default:
  188. WARN_ONCE(1, "Invalid performance counter number (%d)\n", idx);
  189. return 0;
  190. }
  191. return val;
  192. }
  193. static void loongarch_pmu_write_control(unsigned int idx, unsigned int val)
  194. {
  195. switch (idx) {
  196. case 0:
  197. write_csr_perfctrl0(val);
  198. return;
  199. case 1:
  200. write_csr_perfctrl1(val);
  201. return;
  202. case 2:
  203. write_csr_perfctrl2(val);
  204. return;
  205. case 3:
  206. write_csr_perfctrl3(val);
  207. return;
  208. default:
  209. WARN_ONCE(1, "Invalid performance counter number (%d)\n", idx);
  210. return;
  211. }
  212. }
  213. static int loongarch_pmu_alloc_counter(struct cpu_hw_events *cpuc, struct hw_perf_event *hwc)
  214. {
  215. int i;
  216. for (i = 0; i < loongarch_pmu.num_counters; i++) {
  217. if (!test_and_set_bit(i, cpuc->used_mask))
  218. return i;
  219. }
  220. return -EAGAIN;
  221. }
  222. static void loongarch_pmu_enable_event(struct hw_perf_event *evt, int idx)
  223. {
  224. unsigned int cpu;
  225. struct perf_event *event = container_of(evt, struct perf_event, hw);
  226. struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
  227. WARN_ON(idx < 0 || idx >= loongarch_pmu.num_counters);
  228. /* Make sure interrupt enabled. */
  229. cpuc->saved_ctrl[idx] = M_PERFCTL_EVENT(evt->event_base) |
  230. (evt->config_base & M_PERFCTL_CONFIG_MASK) | CSR_PERFCTRL_IE;
  231. cpu = (event->cpu >= 0) ? event->cpu : smp_processor_id();
  232. /*
  233. * We do not actually let the counter run. Leave it until start().
  234. */
  235. pr_debug("Enabling perf counter for CPU%d\n", cpu);
  236. }
  237. static void loongarch_pmu_disable_event(int idx)
  238. {
  239. unsigned long flags;
  240. struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
  241. WARN_ON(idx < 0 || idx >= loongarch_pmu.num_counters);
  242. local_irq_save(flags);
  243. cpuc->saved_ctrl[idx] = loongarch_pmu_read_control(idx) &
  244. ~M_PERFCTL_COUNT_EVENT_WHENEVER;
  245. loongarch_pmu_write_control(idx, cpuc->saved_ctrl[idx]);
  246. local_irq_restore(flags);
  247. }
  248. static int loongarch_pmu_event_set_period(struct perf_event *event,
  249. struct hw_perf_event *hwc,
  250. int idx)
  251. {
  252. int ret = 0;
  253. u64 left = local64_read(&hwc->period_left);
  254. u64 period = hwc->sample_period;
  255. if (unlikely((left + period) & (1ULL << 63))) {
  256. /* left underflowed by more than period. */
  257. left = period;
  258. local64_set(&hwc->period_left, left);
  259. hwc->last_period = period;
  260. ret = 1;
  261. } else if (unlikely((left + period) <= period)) {
  262. /* left underflowed by less than period. */
  263. left += period;
  264. local64_set(&hwc->period_left, left);
  265. hwc->last_period = period;
  266. ret = 1;
  267. }
  268. if (left > loongarch_pmu.max_period) {
  269. left = loongarch_pmu.max_period;
  270. local64_set(&hwc->period_left, left);
  271. }
  272. local64_set(&hwc->prev_count, loongarch_pmu.overflow - left);
  273. loongarch_pmu.write_counter(idx, loongarch_pmu.overflow - left);
  274. perf_event_update_userpage(event);
  275. return ret;
  276. }
  277. static void loongarch_pmu_event_update(struct perf_event *event,
  278. struct hw_perf_event *hwc,
  279. int idx)
  280. {
  281. u64 delta;
  282. u64 prev_raw_count, new_raw_count;
  283. again:
  284. prev_raw_count = local64_read(&hwc->prev_count);
  285. new_raw_count = loongarch_pmu.read_counter(idx);
  286. if (local64_cmpxchg(&hwc->prev_count, prev_raw_count,
  287. new_raw_count) != prev_raw_count)
  288. goto again;
  289. delta = new_raw_count - prev_raw_count;
  290. local64_add(delta, &event->count);
  291. local64_sub(delta, &hwc->period_left);
  292. }
  293. static void loongarch_pmu_start(struct perf_event *event, int flags)
  294. {
  295. struct hw_perf_event *hwc = &event->hw;
  296. if (flags & PERF_EF_RELOAD)
  297. WARN_ON_ONCE(!(hwc->state & PERF_HES_UPTODATE));
  298. hwc->state = 0;
  299. /* Set the period for the event. */
  300. loongarch_pmu_event_set_period(event, hwc, hwc->idx);
  301. /* Enable the event. */
  302. loongarch_pmu_enable_event(hwc, hwc->idx);
  303. }
  304. static void loongarch_pmu_stop(struct perf_event *event, int flags)
  305. {
  306. struct hw_perf_event *hwc = &event->hw;
  307. if (!(hwc->state & PERF_HES_STOPPED)) {
  308. /* We are working on a local event. */
  309. loongarch_pmu_disable_event(hwc->idx);
  310. barrier();
  311. loongarch_pmu_event_update(event, hwc, hwc->idx);
  312. hwc->state |= PERF_HES_STOPPED | PERF_HES_UPTODATE;
  313. }
  314. }
  315. static int loongarch_pmu_add(struct perf_event *event, int flags)
  316. {
  317. int idx, err = 0;
  318. struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
  319. struct hw_perf_event *hwc = &event->hw;
  320. perf_pmu_disable(event->pmu);
  321. /* To look for a free counter for this event. */
  322. idx = loongarch_pmu_alloc_counter(cpuc, hwc);
  323. if (idx < 0) {
  324. err = idx;
  325. goto out;
  326. }
  327. /*
  328. * If there is an event in the counter we are going to use then
  329. * make sure it is disabled.
  330. */
  331. event->hw.idx = idx;
  332. loongarch_pmu_disable_event(idx);
  333. cpuc->events[idx] = event;
  334. hwc->state = PERF_HES_STOPPED | PERF_HES_UPTODATE;
  335. if (flags & PERF_EF_START)
  336. loongarch_pmu_start(event, PERF_EF_RELOAD);
  337. /* Propagate our changes to the userspace mapping. */
  338. perf_event_update_userpage(event);
  339. out:
  340. perf_pmu_enable(event->pmu);
  341. return err;
  342. }
  343. static void loongarch_pmu_del(struct perf_event *event, int flags)
  344. {
  345. struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
  346. struct hw_perf_event *hwc = &event->hw;
  347. int idx = hwc->idx;
  348. WARN_ON(idx < 0 || idx >= loongarch_pmu.num_counters);
  349. loongarch_pmu_stop(event, PERF_EF_UPDATE);
  350. cpuc->events[idx] = NULL;
  351. clear_bit(idx, cpuc->used_mask);
  352. perf_event_update_userpage(event);
  353. }
  354. static void loongarch_pmu_read(struct perf_event *event)
  355. {
  356. struct hw_perf_event *hwc = &event->hw;
  357. /* Don't read disabled counters! */
  358. if (hwc->idx < 0)
  359. return;
  360. loongarch_pmu_event_update(event, hwc, hwc->idx);
  361. }
  362. static void loongarch_pmu_enable(struct pmu *pmu)
  363. {
  364. resume_local_counters();
  365. }
  366. static void loongarch_pmu_disable(struct pmu *pmu)
  367. {
  368. pause_local_counters();
  369. }
  370. static DEFINE_MUTEX(pmu_reserve_mutex);
  371. static atomic_t active_events = ATOMIC_INIT(0);
  372. static void reset_counters(void *arg);
  373. static int __hw_perf_event_init(struct perf_event *event);
  374. static void hw_perf_event_destroy(struct perf_event *event)
  375. {
  376. if (atomic_dec_and_mutex_lock(&active_events, &pmu_reserve_mutex)) {
  377. on_each_cpu(reset_counters, NULL, 1);
  378. free_irq(get_percpu_irq(INT_PCOV), &loongarch_pmu);
  379. mutex_unlock(&pmu_reserve_mutex);
  380. }
  381. }
  382. static void handle_associated_event(struct cpu_hw_events *cpuc, int idx,
  383. struct perf_sample_data *data, struct pt_regs *regs)
  384. {
  385. struct perf_event *event = cpuc->events[idx];
  386. struct hw_perf_event *hwc = &event->hw;
  387. loongarch_pmu_event_update(event, hwc, idx);
  388. data->period = event->hw.last_period;
  389. if (!loongarch_pmu_event_set_period(event, hwc, idx))
  390. return;
  391. if (perf_event_overflow(event, data, regs))
  392. loongarch_pmu_disable_event(idx);
  393. }
  394. static irqreturn_t pmu_handle_irq(int irq, void *dev)
  395. {
  396. int n;
  397. int handled = IRQ_NONE;
  398. uint64_t counter;
  399. struct pt_regs *regs;
  400. struct perf_sample_data data;
  401. struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
  402. /*
  403. * First we pause the local counters, so that when we are locked
  404. * here, the counters are all paused. When it gets locked due to
  405. * perf_disable(), the timer interrupt handler will be delayed.
  406. *
  407. * See also loongarch_pmu_start().
  408. */
  409. pause_local_counters();
  410. regs = get_irq_regs();
  411. perf_sample_data_init(&data, 0, 0);
  412. for (n = 0; n < loongarch_pmu.num_counters; n++) {
  413. if (test_bit(n, cpuc->used_mask)) {
  414. counter = loongarch_pmu.read_counter(n);
  415. if (counter & loongarch_pmu.overflow) {
  416. handle_associated_event(cpuc, n, &data, regs);
  417. handled = IRQ_HANDLED;
  418. }
  419. }
  420. }
  421. resume_local_counters();
  422. /*
  423. * Do all the work for the pending perf events. We can do this
  424. * in here because the performance counter interrupt is a regular
  425. * interrupt, not NMI.
  426. */
  427. if (handled == IRQ_HANDLED)
  428. irq_work_run();
  429. return handled;
  430. }
  431. static int loongarch_pmu_event_init(struct perf_event *event)
  432. {
  433. int r, irq;
  434. unsigned long flags;
  435. /* does not support taken branch sampling */
  436. if (has_branch_stack(event))
  437. return -EOPNOTSUPP;
  438. switch (event->attr.type) {
  439. case PERF_TYPE_RAW:
  440. case PERF_TYPE_HARDWARE:
  441. case PERF_TYPE_HW_CACHE:
  442. break;
  443. default:
  444. /* Init it to avoid false validate_group */
  445. event->hw.event_base = 0xffffffff;
  446. return -ENOENT;
  447. }
  448. if (event->cpu >= 0 && !cpu_online(event->cpu))
  449. return -ENODEV;
  450. irq = get_percpu_irq(INT_PCOV);
  451. flags = IRQF_PERCPU | IRQF_NOBALANCING | IRQF_NO_THREAD | IRQF_NO_SUSPEND | IRQF_SHARED;
  452. if (!atomic_inc_not_zero(&active_events)) {
  453. mutex_lock(&pmu_reserve_mutex);
  454. if (atomic_read(&active_events) == 0) {
  455. r = request_irq(irq, pmu_handle_irq, flags, "Perf_PMU", &loongarch_pmu);
  456. if (r < 0) {
  457. mutex_unlock(&pmu_reserve_mutex);
  458. pr_warn("PMU IRQ request failed\n");
  459. return -ENODEV;
  460. }
  461. }
  462. atomic_inc(&active_events);
  463. mutex_unlock(&pmu_reserve_mutex);
  464. }
  465. return __hw_perf_event_init(event);
  466. }
  467. static struct pmu pmu = {
  468. .pmu_enable = loongarch_pmu_enable,
  469. .pmu_disable = loongarch_pmu_disable,
  470. .event_init = loongarch_pmu_event_init,
  471. .add = loongarch_pmu_add,
  472. .del = loongarch_pmu_del,
  473. .start = loongarch_pmu_start,
  474. .stop = loongarch_pmu_stop,
  475. .read = loongarch_pmu_read,
  476. };
  477. static unsigned int loongarch_pmu_perf_event_encode(const struct loongarch_perf_event *pev)
  478. {
  479. return M_PERFCTL_EVENT(pev->event_id);
  480. }
  481. static const struct loongarch_perf_event *loongarch_pmu_map_general_event(int idx)
  482. {
  483. const struct loongarch_perf_event *pev;
  484. pev = &(*loongarch_pmu.general_event_map)[idx];
  485. if (pev->event_id == HW_OP_UNSUPPORTED)
  486. return ERR_PTR(-ENOENT);
  487. return pev;
  488. }
  489. static const struct loongarch_perf_event *loongarch_pmu_map_cache_event(u64 config)
  490. {
  491. unsigned int cache_type, cache_op, cache_result;
  492. const struct loongarch_perf_event *pev;
  493. cache_type = (config >> 0) & 0xff;
  494. if (cache_type >= PERF_COUNT_HW_CACHE_MAX)
  495. return ERR_PTR(-EINVAL);
  496. cache_op = (config >> 8) & 0xff;
  497. if (cache_op >= PERF_COUNT_HW_CACHE_OP_MAX)
  498. return ERR_PTR(-EINVAL);
  499. cache_result = (config >> 16) & 0xff;
  500. if (cache_result >= PERF_COUNT_HW_CACHE_RESULT_MAX)
  501. return ERR_PTR(-EINVAL);
  502. pev = &((*loongarch_pmu.cache_event_map)
  503. [cache_type]
  504. [cache_op]
  505. [cache_result]);
  506. if (pev->event_id == CACHE_OP_UNSUPPORTED)
  507. return ERR_PTR(-ENOENT);
  508. return pev;
  509. }
  510. static int validate_group(struct perf_event *event)
  511. {
  512. struct cpu_hw_events fake_cpuc;
  513. struct perf_event *sibling, *leader = event->group_leader;
  514. memset(&fake_cpuc, 0, sizeof(fake_cpuc));
  515. if (loongarch_pmu_alloc_counter(&fake_cpuc, &leader->hw) < 0)
  516. return -EINVAL;
  517. for_each_sibling_event(sibling, leader) {
  518. if (loongarch_pmu_alloc_counter(&fake_cpuc, &sibling->hw) < 0)
  519. return -EINVAL;
  520. }
  521. if (loongarch_pmu_alloc_counter(&fake_cpuc, &event->hw) < 0)
  522. return -EINVAL;
  523. return 0;
  524. }
  525. static void reset_counters(void *arg)
  526. {
  527. int n;
  528. int counters = loongarch_pmu.num_counters;
  529. for (n = 0; n < counters; n++) {
  530. loongarch_pmu_write_control(n, 0);
  531. loongarch_pmu.write_counter(n, 0);
  532. }
  533. }
  534. static const struct loongarch_perf_event loongson_event_map[PERF_COUNT_HW_MAX] = {
  535. PERF_MAP_ALL_UNSUPPORTED,
  536. [PERF_COUNT_HW_CPU_CYCLES] = { 0x00 },
  537. [PERF_COUNT_HW_INSTRUCTIONS] = { 0x01 },
  538. [PERF_COUNT_HW_CACHE_REFERENCES] = { 0x08 },
  539. [PERF_COUNT_HW_CACHE_MISSES] = { 0x09 },
  540. [PERF_COUNT_HW_BRANCH_INSTRUCTIONS] = { 0x02 },
  541. [PERF_COUNT_HW_BRANCH_MISSES] = { 0x03 },
  542. };
  543. static const struct loongarch_perf_event loongson_cache_map
  544. [PERF_COUNT_HW_CACHE_MAX]
  545. [PERF_COUNT_HW_CACHE_OP_MAX]
  546. [PERF_COUNT_HW_CACHE_RESULT_MAX] = {
  547. PERF_CACHE_MAP_ALL_UNSUPPORTED,
  548. [C(L1D)] = {
  549. /*
  550. * Like some other architectures (e.g. ARM), the performance
  551. * counters don't differentiate between read and write
  552. * accesses/misses, so this isn't strictly correct, but it's the
  553. * best we can do. Writes and reads get combined.
  554. */
  555. [C(OP_READ)] = {
  556. [C(RESULT_ACCESS)] = { 0x8 },
  557. [C(RESULT_MISS)] = { 0x9 },
  558. },
  559. [C(OP_WRITE)] = {
  560. [C(RESULT_ACCESS)] = { 0x8 },
  561. [C(RESULT_MISS)] = { 0x9 },
  562. },
  563. [C(OP_PREFETCH)] = {
  564. [C(RESULT_ACCESS)] = { 0xaa },
  565. [C(RESULT_MISS)] = { 0xa9 },
  566. },
  567. },
  568. [C(L1I)] = {
  569. [C(OP_READ)] = {
  570. [C(RESULT_ACCESS)] = { 0x6 },
  571. [C(RESULT_MISS)] = { 0x7 },
  572. },
  573. },
  574. [C(LL)] = {
  575. [C(OP_READ)] = {
  576. [C(RESULT_ACCESS)] = { 0xc },
  577. [C(RESULT_MISS)] = { 0xd },
  578. },
  579. [C(OP_WRITE)] = {
  580. [C(RESULT_ACCESS)] = { 0xc },
  581. [C(RESULT_MISS)] = { 0xd },
  582. },
  583. },
  584. [C(ITLB)] = {
  585. [C(OP_READ)] = {
  586. [C(RESULT_MISS)] = { 0x3b },
  587. },
  588. },
  589. [C(DTLB)] = {
  590. [C(OP_READ)] = {
  591. [C(RESULT_ACCESS)] = { 0x4 },
  592. [C(RESULT_MISS)] = { 0x3c },
  593. },
  594. [C(OP_WRITE)] = {
  595. [C(RESULT_ACCESS)] = { 0x4 },
  596. [C(RESULT_MISS)] = { 0x3c },
  597. },
  598. },
  599. [C(BPU)] = {
  600. /* Using the same code for *HW_BRANCH* */
  601. [C(OP_READ)] = {
  602. [C(RESULT_ACCESS)] = { 0x02 },
  603. [C(RESULT_MISS)] = { 0x03 },
  604. },
  605. },
  606. };
  607. static int __hw_perf_event_init(struct perf_event *event)
  608. {
  609. int err;
  610. struct hw_perf_event *hwc = &event->hw;
  611. struct perf_event_attr *attr = &event->attr;
  612. const struct loongarch_perf_event *pev;
  613. /* Returning LoongArch event descriptor for generic perf event. */
  614. if (PERF_TYPE_HARDWARE == event->attr.type) {
  615. if (event->attr.config >= PERF_COUNT_HW_MAX)
  616. return -EINVAL;
  617. pev = loongarch_pmu_map_general_event(event->attr.config);
  618. } else if (PERF_TYPE_HW_CACHE == event->attr.type) {
  619. pev = loongarch_pmu_map_cache_event(event->attr.config);
  620. } else if (PERF_TYPE_RAW == event->attr.type) {
  621. /* We are working on the global raw event. */
  622. mutex_lock(&raw_event_mutex);
  623. pev = loongarch_pmu.map_raw_event(event->attr.config);
  624. } else {
  625. /* The event type is not (yet) supported. */
  626. return -EOPNOTSUPP;
  627. }
  628. if (IS_ERR(pev)) {
  629. if (PERF_TYPE_RAW == event->attr.type)
  630. mutex_unlock(&raw_event_mutex);
  631. return PTR_ERR(pev);
  632. }
  633. /*
  634. * We allow max flexibility on how each individual counter shared
  635. * by the single CPU operates (the mode exclusion and the range).
  636. */
  637. hwc->config_base = CSR_PERFCTRL_IE;
  638. hwc->event_base = loongarch_pmu_perf_event_encode(pev);
  639. if (PERF_TYPE_RAW == event->attr.type)
  640. mutex_unlock(&raw_event_mutex);
  641. if (!attr->exclude_user) {
  642. hwc->config_base |= CSR_PERFCTRL_PLV3;
  643. hwc->config_base |= CSR_PERFCTRL_PLV2;
  644. }
  645. if (!attr->exclude_kernel) {
  646. hwc->config_base |= CSR_PERFCTRL_PLV0;
  647. }
  648. if (!attr->exclude_hv) {
  649. hwc->config_base |= CSR_PERFCTRL_PLV1;
  650. }
  651. hwc->config_base &= M_PERFCTL_CONFIG_MASK;
  652. /*
  653. * The event can belong to another cpu. We do not assign a local
  654. * counter for it for now.
  655. */
  656. hwc->idx = -1;
  657. hwc->config = 0;
  658. if (!hwc->sample_period) {
  659. hwc->sample_period = loongarch_pmu.max_period;
  660. hwc->last_period = hwc->sample_period;
  661. local64_set(&hwc->period_left, hwc->sample_period);
  662. }
  663. err = 0;
  664. if (event->group_leader != event)
  665. err = validate_group(event);
  666. event->destroy = hw_perf_event_destroy;
  667. if (err)
  668. event->destroy(event);
  669. return err;
  670. }
  671. static void pause_local_counters(void)
  672. {
  673. unsigned long flags;
  674. int ctr = loongarch_pmu.num_counters;
  675. struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
  676. local_irq_save(flags);
  677. do {
  678. ctr--;
  679. cpuc->saved_ctrl[ctr] = loongarch_pmu_read_control(ctr);
  680. loongarch_pmu_write_control(ctr, cpuc->saved_ctrl[ctr] &
  681. ~M_PERFCTL_COUNT_EVENT_WHENEVER);
  682. } while (ctr > 0);
  683. local_irq_restore(flags);
  684. }
  685. static void resume_local_counters(void)
  686. {
  687. int ctr = loongarch_pmu.num_counters;
  688. struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
  689. do {
  690. ctr--;
  691. loongarch_pmu_write_control(ctr, cpuc->saved_ctrl[ctr]);
  692. } while (ctr > 0);
  693. }
  694. static const struct loongarch_perf_event *loongarch_pmu_map_raw_event(u64 config)
  695. {
  696. raw_event.event_id = M_PERFCTL_EVENT(config);
  697. return &raw_event;
  698. }
  699. static int __init init_hw_perf_events(void)
  700. {
  701. int counters;
  702. if (!cpu_has_pmp)
  703. return -ENODEV;
  704. pr_info("Performance counters: ");
  705. counters = ((read_cpucfg(LOONGARCH_CPUCFG6) & CPUCFG6_PMNUM) >> 4) + 1;
  706. loongarch_pmu.num_counters = counters;
  707. loongarch_pmu.max_period = (1ULL << 63) - 1;
  708. loongarch_pmu.valid_count = (1ULL << 63) - 1;
  709. loongarch_pmu.overflow = 1ULL << 63;
  710. loongarch_pmu.name = "loongarch/loongson64";
  711. loongarch_pmu.read_counter = loongarch_pmu_read_counter;
  712. loongarch_pmu.write_counter = loongarch_pmu_write_counter;
  713. loongarch_pmu.map_raw_event = loongarch_pmu_map_raw_event;
  714. loongarch_pmu.general_event_map = &loongson_event_map;
  715. loongarch_pmu.cache_event_map = &loongson_cache_map;
  716. on_each_cpu(reset_counters, NULL, 1);
  717. pr_cont("%s PMU enabled, %d %d-bit counters available to each CPU.\n",
  718. loongarch_pmu.name, counters, 64);
  719. perf_pmu_register(&pmu, "cpu", PERF_TYPE_RAW);
  720. return 0;
  721. }
  722. pure_initcall(init_hw_perf_events);