perf_event.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887
  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 int get_pmc_irq(void)
  373. {
  374. struct irq_domain *d = irq_find_matching_fwnode(cpuintc_handle, DOMAIN_BUS_ANY);
  375. if (d)
  376. return irq_create_mapping(d, EXCCODE_PMC - EXCCODE_INT_START);
  377. return -EINVAL;
  378. }
  379. static void reset_counters(void *arg);
  380. static int __hw_perf_event_init(struct perf_event *event);
  381. static void hw_perf_event_destroy(struct perf_event *event)
  382. {
  383. if (atomic_dec_and_mutex_lock(&active_events, &pmu_reserve_mutex)) {
  384. on_each_cpu(reset_counters, NULL, 1);
  385. free_irq(get_pmc_irq(), &loongarch_pmu);
  386. mutex_unlock(&pmu_reserve_mutex);
  387. }
  388. }
  389. static void handle_associated_event(struct cpu_hw_events *cpuc, int idx,
  390. struct perf_sample_data *data, struct pt_regs *regs)
  391. {
  392. struct perf_event *event = cpuc->events[idx];
  393. struct hw_perf_event *hwc = &event->hw;
  394. loongarch_pmu_event_update(event, hwc, idx);
  395. data->period = event->hw.last_period;
  396. if (!loongarch_pmu_event_set_period(event, hwc, idx))
  397. return;
  398. if (perf_event_overflow(event, data, regs))
  399. loongarch_pmu_disable_event(idx);
  400. }
  401. static irqreturn_t pmu_handle_irq(int irq, void *dev)
  402. {
  403. int n;
  404. int handled = IRQ_NONE;
  405. uint64_t counter;
  406. struct pt_regs *regs;
  407. struct perf_sample_data data;
  408. struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
  409. /*
  410. * First we pause the local counters, so that when we are locked
  411. * here, the counters are all paused. When it gets locked due to
  412. * perf_disable(), the timer interrupt handler will be delayed.
  413. *
  414. * See also loongarch_pmu_start().
  415. */
  416. pause_local_counters();
  417. regs = get_irq_regs();
  418. perf_sample_data_init(&data, 0, 0);
  419. for (n = 0; n < loongarch_pmu.num_counters; n++) {
  420. if (test_bit(n, cpuc->used_mask)) {
  421. counter = loongarch_pmu.read_counter(n);
  422. if (counter & loongarch_pmu.overflow) {
  423. handle_associated_event(cpuc, n, &data, regs);
  424. handled = IRQ_HANDLED;
  425. }
  426. }
  427. }
  428. resume_local_counters();
  429. /*
  430. * Do all the work for the pending perf events. We can do this
  431. * in here because the performance counter interrupt is a regular
  432. * interrupt, not NMI.
  433. */
  434. if (handled == IRQ_HANDLED)
  435. irq_work_run();
  436. return handled;
  437. }
  438. static int loongarch_pmu_event_init(struct perf_event *event)
  439. {
  440. int r, irq;
  441. unsigned long flags;
  442. /* does not support taken branch sampling */
  443. if (has_branch_stack(event))
  444. return -EOPNOTSUPP;
  445. switch (event->attr.type) {
  446. case PERF_TYPE_RAW:
  447. case PERF_TYPE_HARDWARE:
  448. case PERF_TYPE_HW_CACHE:
  449. break;
  450. default:
  451. /* Init it to avoid false validate_group */
  452. event->hw.event_base = 0xffffffff;
  453. return -ENOENT;
  454. }
  455. if (event->cpu >= 0 && !cpu_online(event->cpu))
  456. return -ENODEV;
  457. irq = get_pmc_irq();
  458. flags = IRQF_PERCPU | IRQF_NOBALANCING | IRQF_NO_THREAD | IRQF_NO_SUSPEND | IRQF_SHARED;
  459. if (!atomic_inc_not_zero(&active_events)) {
  460. mutex_lock(&pmu_reserve_mutex);
  461. if (atomic_read(&active_events) == 0) {
  462. r = request_irq(irq, pmu_handle_irq, flags, "Perf_PMU", &loongarch_pmu);
  463. if (r < 0) {
  464. mutex_unlock(&pmu_reserve_mutex);
  465. pr_warn("PMU IRQ request failed\n");
  466. return -ENODEV;
  467. }
  468. }
  469. atomic_inc(&active_events);
  470. mutex_unlock(&pmu_reserve_mutex);
  471. }
  472. return __hw_perf_event_init(event);
  473. }
  474. static struct pmu pmu = {
  475. .pmu_enable = loongarch_pmu_enable,
  476. .pmu_disable = loongarch_pmu_disable,
  477. .event_init = loongarch_pmu_event_init,
  478. .add = loongarch_pmu_add,
  479. .del = loongarch_pmu_del,
  480. .start = loongarch_pmu_start,
  481. .stop = loongarch_pmu_stop,
  482. .read = loongarch_pmu_read,
  483. };
  484. static unsigned int loongarch_pmu_perf_event_encode(const struct loongarch_perf_event *pev)
  485. {
  486. return M_PERFCTL_EVENT(pev->event_id);
  487. }
  488. static const struct loongarch_perf_event *loongarch_pmu_map_general_event(int idx)
  489. {
  490. const struct loongarch_perf_event *pev;
  491. pev = &(*loongarch_pmu.general_event_map)[idx];
  492. if (pev->event_id == HW_OP_UNSUPPORTED)
  493. return ERR_PTR(-ENOENT);
  494. return pev;
  495. }
  496. static const struct loongarch_perf_event *loongarch_pmu_map_cache_event(u64 config)
  497. {
  498. unsigned int cache_type, cache_op, cache_result;
  499. const struct loongarch_perf_event *pev;
  500. cache_type = (config >> 0) & 0xff;
  501. if (cache_type >= PERF_COUNT_HW_CACHE_MAX)
  502. return ERR_PTR(-EINVAL);
  503. cache_op = (config >> 8) & 0xff;
  504. if (cache_op >= PERF_COUNT_HW_CACHE_OP_MAX)
  505. return ERR_PTR(-EINVAL);
  506. cache_result = (config >> 16) & 0xff;
  507. if (cache_result >= PERF_COUNT_HW_CACHE_RESULT_MAX)
  508. return ERR_PTR(-EINVAL);
  509. pev = &((*loongarch_pmu.cache_event_map)
  510. [cache_type]
  511. [cache_op]
  512. [cache_result]);
  513. if (pev->event_id == CACHE_OP_UNSUPPORTED)
  514. return ERR_PTR(-ENOENT);
  515. return pev;
  516. }
  517. static int validate_group(struct perf_event *event)
  518. {
  519. struct cpu_hw_events fake_cpuc;
  520. struct perf_event *sibling, *leader = event->group_leader;
  521. memset(&fake_cpuc, 0, sizeof(fake_cpuc));
  522. if (loongarch_pmu_alloc_counter(&fake_cpuc, &leader->hw) < 0)
  523. return -EINVAL;
  524. for_each_sibling_event(sibling, leader) {
  525. if (loongarch_pmu_alloc_counter(&fake_cpuc, &sibling->hw) < 0)
  526. return -EINVAL;
  527. }
  528. if (loongarch_pmu_alloc_counter(&fake_cpuc, &event->hw) < 0)
  529. return -EINVAL;
  530. return 0;
  531. }
  532. static void reset_counters(void *arg)
  533. {
  534. int n;
  535. int counters = loongarch_pmu.num_counters;
  536. for (n = 0; n < counters; n++) {
  537. loongarch_pmu_write_control(n, 0);
  538. loongarch_pmu.write_counter(n, 0);
  539. }
  540. }
  541. static const struct loongarch_perf_event loongson_event_map[PERF_COUNT_HW_MAX] = {
  542. PERF_MAP_ALL_UNSUPPORTED,
  543. [PERF_COUNT_HW_CPU_CYCLES] = { 0x00 },
  544. [PERF_COUNT_HW_INSTRUCTIONS] = { 0x01 },
  545. [PERF_COUNT_HW_CACHE_REFERENCES] = { 0x08 },
  546. [PERF_COUNT_HW_CACHE_MISSES] = { 0x09 },
  547. [PERF_COUNT_HW_BRANCH_INSTRUCTIONS] = { 0x02 },
  548. [PERF_COUNT_HW_BRANCH_MISSES] = { 0x03 },
  549. };
  550. static const struct loongarch_perf_event loongson_cache_map
  551. [PERF_COUNT_HW_CACHE_MAX]
  552. [PERF_COUNT_HW_CACHE_OP_MAX]
  553. [PERF_COUNT_HW_CACHE_RESULT_MAX] = {
  554. PERF_CACHE_MAP_ALL_UNSUPPORTED,
  555. [C(L1D)] = {
  556. /*
  557. * Like some other architectures (e.g. ARM), the performance
  558. * counters don't differentiate between read and write
  559. * accesses/misses, so this isn't strictly correct, but it's the
  560. * best we can do. Writes and reads get combined.
  561. */
  562. [C(OP_READ)] = {
  563. [C(RESULT_ACCESS)] = { 0x8 },
  564. [C(RESULT_MISS)] = { 0x9 },
  565. },
  566. [C(OP_WRITE)] = {
  567. [C(RESULT_ACCESS)] = { 0x8 },
  568. [C(RESULT_MISS)] = { 0x9 },
  569. },
  570. [C(OP_PREFETCH)] = {
  571. [C(RESULT_ACCESS)] = { 0xaa },
  572. [C(RESULT_MISS)] = { 0xa9 },
  573. },
  574. },
  575. [C(L1I)] = {
  576. [C(OP_READ)] = {
  577. [C(RESULT_ACCESS)] = { 0x6 },
  578. [C(RESULT_MISS)] = { 0x7 },
  579. },
  580. },
  581. [C(LL)] = {
  582. [C(OP_READ)] = {
  583. [C(RESULT_ACCESS)] = { 0xc },
  584. [C(RESULT_MISS)] = { 0xd },
  585. },
  586. [C(OP_WRITE)] = {
  587. [C(RESULT_ACCESS)] = { 0xc },
  588. [C(RESULT_MISS)] = { 0xd },
  589. },
  590. },
  591. [C(ITLB)] = {
  592. [C(OP_READ)] = {
  593. [C(RESULT_MISS)] = { 0x3b },
  594. },
  595. },
  596. [C(DTLB)] = {
  597. [C(OP_READ)] = {
  598. [C(RESULT_ACCESS)] = { 0x4 },
  599. [C(RESULT_MISS)] = { 0x3c },
  600. },
  601. [C(OP_WRITE)] = {
  602. [C(RESULT_ACCESS)] = { 0x4 },
  603. [C(RESULT_MISS)] = { 0x3c },
  604. },
  605. },
  606. [C(BPU)] = {
  607. /* Using the same code for *HW_BRANCH* */
  608. [C(OP_READ)] = {
  609. [C(RESULT_ACCESS)] = { 0x02 },
  610. [C(RESULT_MISS)] = { 0x03 },
  611. },
  612. },
  613. };
  614. static int __hw_perf_event_init(struct perf_event *event)
  615. {
  616. int err;
  617. struct hw_perf_event *hwc = &event->hw;
  618. struct perf_event_attr *attr = &event->attr;
  619. const struct loongarch_perf_event *pev;
  620. /* Returning LoongArch event descriptor for generic perf event. */
  621. if (PERF_TYPE_HARDWARE == event->attr.type) {
  622. if (event->attr.config >= PERF_COUNT_HW_MAX)
  623. return -EINVAL;
  624. pev = loongarch_pmu_map_general_event(event->attr.config);
  625. } else if (PERF_TYPE_HW_CACHE == event->attr.type) {
  626. pev = loongarch_pmu_map_cache_event(event->attr.config);
  627. } else if (PERF_TYPE_RAW == event->attr.type) {
  628. /* We are working on the global raw event. */
  629. mutex_lock(&raw_event_mutex);
  630. pev = loongarch_pmu.map_raw_event(event->attr.config);
  631. } else {
  632. /* The event type is not (yet) supported. */
  633. return -EOPNOTSUPP;
  634. }
  635. if (IS_ERR(pev)) {
  636. if (PERF_TYPE_RAW == event->attr.type)
  637. mutex_unlock(&raw_event_mutex);
  638. return PTR_ERR(pev);
  639. }
  640. /*
  641. * We allow max flexibility on how each individual counter shared
  642. * by the single CPU operates (the mode exclusion and the range).
  643. */
  644. hwc->config_base = CSR_PERFCTRL_IE;
  645. hwc->event_base = loongarch_pmu_perf_event_encode(pev);
  646. if (PERF_TYPE_RAW == event->attr.type)
  647. mutex_unlock(&raw_event_mutex);
  648. if (!attr->exclude_user) {
  649. hwc->config_base |= CSR_PERFCTRL_PLV3;
  650. hwc->config_base |= CSR_PERFCTRL_PLV2;
  651. }
  652. if (!attr->exclude_kernel) {
  653. hwc->config_base |= CSR_PERFCTRL_PLV0;
  654. }
  655. if (!attr->exclude_hv) {
  656. hwc->config_base |= CSR_PERFCTRL_PLV1;
  657. }
  658. hwc->config_base &= M_PERFCTL_CONFIG_MASK;
  659. /*
  660. * The event can belong to another cpu. We do not assign a local
  661. * counter for it for now.
  662. */
  663. hwc->idx = -1;
  664. hwc->config = 0;
  665. if (!hwc->sample_period) {
  666. hwc->sample_period = loongarch_pmu.max_period;
  667. hwc->last_period = hwc->sample_period;
  668. local64_set(&hwc->period_left, hwc->sample_period);
  669. }
  670. err = 0;
  671. if (event->group_leader != event)
  672. err = validate_group(event);
  673. event->destroy = hw_perf_event_destroy;
  674. if (err)
  675. event->destroy(event);
  676. return err;
  677. }
  678. static void pause_local_counters(void)
  679. {
  680. unsigned long flags;
  681. int ctr = loongarch_pmu.num_counters;
  682. struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
  683. local_irq_save(flags);
  684. do {
  685. ctr--;
  686. cpuc->saved_ctrl[ctr] = loongarch_pmu_read_control(ctr);
  687. loongarch_pmu_write_control(ctr, cpuc->saved_ctrl[ctr] &
  688. ~M_PERFCTL_COUNT_EVENT_WHENEVER);
  689. } while (ctr > 0);
  690. local_irq_restore(flags);
  691. }
  692. static void resume_local_counters(void)
  693. {
  694. int ctr = loongarch_pmu.num_counters;
  695. struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
  696. do {
  697. ctr--;
  698. loongarch_pmu_write_control(ctr, cpuc->saved_ctrl[ctr]);
  699. } while (ctr > 0);
  700. }
  701. static const struct loongarch_perf_event *loongarch_pmu_map_raw_event(u64 config)
  702. {
  703. raw_event.event_id = M_PERFCTL_EVENT(config);
  704. return &raw_event;
  705. }
  706. static int __init init_hw_perf_events(void)
  707. {
  708. int counters;
  709. if (!cpu_has_pmp)
  710. return -ENODEV;
  711. pr_info("Performance counters: ");
  712. counters = ((read_cpucfg(LOONGARCH_CPUCFG6) & CPUCFG6_PMNUM) >> 4) + 1;
  713. loongarch_pmu.num_counters = counters;
  714. loongarch_pmu.max_period = (1ULL << 63) - 1;
  715. loongarch_pmu.valid_count = (1ULL << 63) - 1;
  716. loongarch_pmu.overflow = 1ULL << 63;
  717. loongarch_pmu.name = "loongarch/loongson64";
  718. loongarch_pmu.read_counter = loongarch_pmu_read_counter;
  719. loongarch_pmu.write_counter = loongarch_pmu_write_counter;
  720. loongarch_pmu.map_raw_event = loongarch_pmu_map_raw_event;
  721. loongarch_pmu.general_event_map = &loongson_event_map;
  722. loongarch_pmu.cache_event_map = &loongson_cache_map;
  723. on_each_cpu(reset_counters, NULL, 1);
  724. pr_cont("%s PMU enabled, %d %d-bit counters available to each CPU.\n",
  725. loongarch_pmu.name, counters, 64);
  726. perf_pmu_register(&pmu, "cpu", PERF_TYPE_RAW);
  727. return 0;
  728. }
  729. early_initcall(init_hw_perf_events);