perf_event_mipsxx.c 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609
  1. /*
  2. * Linux performance counter support for MIPS.
  3. *
  4. * Copyright (C) 2010 MIPS Technologies, Inc.
  5. * Copyright (C) 2011 Cavium Networks, Inc.
  6. * Author: Deng-Cheng Zhu
  7. *
  8. * This code is based on the implementation for ARM, which is in turn
  9. * based on the sparc64 perf event code and the x86 code. Performance
  10. * counter access is based on the MIPS Oprofile code. And the callchain
  11. * support references the code of MIPS stacktrace.c.
  12. *
  13. * This program is free software; you can redistribute it and/or modify
  14. * it under the terms of the GNU General Public License version 2 as
  15. * published by the Free Software Foundation.
  16. */
  17. #include <linux/cpumask.h>
  18. #include <linux/interrupt.h>
  19. #include <linux/smp.h>
  20. #include <linux/kernel.h>
  21. #include <linux/perf_event.h>
  22. #include <linux/uaccess.h>
  23. #include <asm/irq.h>
  24. #include <asm/irq_regs.h>
  25. #include <asm/stacktrace.h>
  26. #include <asm/time.h> /* For perf_irq */
  27. #define MIPS_MAX_HWEVENTS 4
  28. struct cpu_hw_events {
  29. /* Array of events on this cpu. */
  30. struct perf_event *events[MIPS_MAX_HWEVENTS];
  31. /*
  32. * Set the bit (indexed by the counter number) when the counter
  33. * is used for an event.
  34. */
  35. unsigned long used_mask[BITS_TO_LONGS(MIPS_MAX_HWEVENTS)];
  36. /*
  37. * Software copy of the control register for each performance counter.
  38. * MIPS CPUs vary in performance counters. They use this differently,
  39. * and even may not use it.
  40. */
  41. unsigned int saved_ctrl[MIPS_MAX_HWEVENTS];
  42. };
  43. DEFINE_PER_CPU(struct cpu_hw_events, cpu_hw_events) = {
  44. .saved_ctrl = {0},
  45. };
  46. /* The description of MIPS performance events. */
  47. struct mips_perf_event {
  48. unsigned int event_id;
  49. /*
  50. * MIPS performance counters are indexed starting from 0.
  51. * CNTR_EVEN indicates the indexes of the counters to be used are
  52. * even numbers.
  53. */
  54. unsigned int cntr_mask;
  55. #define CNTR_EVEN 0x55555555
  56. #define CNTR_ODD 0xaaaaaaaa
  57. #define CNTR_ALL 0xffffffff
  58. #ifdef CONFIG_MIPS_MT_SMP
  59. enum {
  60. T = 0,
  61. V = 1,
  62. P = 2,
  63. } range;
  64. #else
  65. #define T
  66. #define V
  67. #define P
  68. #endif
  69. };
  70. static struct mips_perf_event raw_event;
  71. static DEFINE_MUTEX(raw_event_mutex);
  72. #define UNSUPPORTED_PERF_EVENT_ID 0xffffffff
  73. #define C(x) PERF_COUNT_HW_CACHE_##x
  74. struct mips_pmu {
  75. u64 max_period;
  76. u64 valid_count;
  77. u64 overflow;
  78. const char *name;
  79. int irq;
  80. u64 (*read_counter)(unsigned int idx);
  81. void (*write_counter)(unsigned int idx, u64 val);
  82. const struct mips_perf_event *(*map_raw_event)(u64 config);
  83. const struct mips_perf_event (*general_event_map)[PERF_COUNT_HW_MAX];
  84. const struct mips_perf_event (*cache_event_map)
  85. [PERF_COUNT_HW_CACHE_MAX]
  86. [PERF_COUNT_HW_CACHE_OP_MAX]
  87. [PERF_COUNT_HW_CACHE_RESULT_MAX];
  88. unsigned int num_counters;
  89. };
  90. static struct mips_pmu mipspmu;
  91. #define M_CONFIG1_PC (1 << 4)
  92. #define M_PERFCTL_EXL (1 << 0)
  93. #define M_PERFCTL_KERNEL (1 << 1)
  94. #define M_PERFCTL_SUPERVISOR (1 << 2)
  95. #define M_PERFCTL_USER (1 << 3)
  96. #define M_PERFCTL_INTERRUPT_ENABLE (1 << 4)
  97. #define M_PERFCTL_EVENT(event) (((event) & 0x3ff) << 5)
  98. #define M_PERFCTL_VPEID(vpe) ((vpe) << 16)
  99. #define M_PERFCTL_MT_EN(filter) ((filter) << 20)
  100. #define M_TC_EN_ALL M_PERFCTL_MT_EN(0)
  101. #define M_TC_EN_VPE M_PERFCTL_MT_EN(1)
  102. #define M_TC_EN_TC M_PERFCTL_MT_EN(2)
  103. #define M_PERFCTL_TCID(tcid) ((tcid) << 22)
  104. #define M_PERFCTL_WIDE (1 << 30)
  105. #define M_PERFCTL_MORE (1 << 31)
  106. #define M_PERFCTL_COUNT_EVENT_WHENEVER (M_PERFCTL_EXL | \
  107. M_PERFCTL_KERNEL | \
  108. M_PERFCTL_USER | \
  109. M_PERFCTL_SUPERVISOR | \
  110. M_PERFCTL_INTERRUPT_ENABLE)
  111. #ifdef CONFIG_MIPS_MT_SMP
  112. #define M_PERFCTL_CONFIG_MASK 0x3fff801f
  113. #else
  114. #define M_PERFCTL_CONFIG_MASK 0x1f
  115. #endif
  116. #define M_PERFCTL_EVENT_MASK 0xfe0
  117. #ifdef CONFIG_MIPS_MT_SMP
  118. static int cpu_has_mipsmt_pertccounters;
  119. static DEFINE_RWLOCK(pmuint_rwlock);
  120. /*
  121. * FIXME: For VSMP, vpe_id() is redefined for Perf-events, because
  122. * cpu_data[cpuid].vpe_id reports 0 for _both_ CPUs.
  123. */
  124. #if defined(CONFIG_HW_PERF_EVENTS)
  125. #define vpe_id() (cpu_has_mipsmt_pertccounters ? \
  126. 0 : smp_processor_id())
  127. #else
  128. #define vpe_id() (cpu_has_mipsmt_pertccounters ? \
  129. 0 : cpu_data[smp_processor_id()].vpe_id)
  130. #endif
  131. /* Copied from op_model_mipsxx.c */
  132. static unsigned int vpe_shift(void)
  133. {
  134. if (num_possible_cpus() > 1)
  135. return 1;
  136. return 0;
  137. }
  138. static unsigned int counters_total_to_per_cpu(unsigned int counters)
  139. {
  140. return counters >> vpe_shift();
  141. }
  142. static unsigned int counters_per_cpu_to_total(unsigned int counters)
  143. {
  144. return counters << vpe_shift();
  145. }
  146. #else /* !CONFIG_MIPS_MT_SMP */
  147. #define vpe_id() 0
  148. #endif /* CONFIG_MIPS_MT_SMP */
  149. static void resume_local_counters(void);
  150. static void pause_local_counters(void);
  151. static irqreturn_t mipsxx_pmu_handle_irq(int, void *);
  152. static int mipsxx_pmu_handle_shared_irq(void);
  153. static unsigned int mipsxx_pmu_swizzle_perf_idx(unsigned int idx)
  154. {
  155. if (vpe_id() == 1)
  156. idx = (idx + 2) & 3;
  157. return idx;
  158. }
  159. static u64 mipsxx_pmu_read_counter(unsigned int idx)
  160. {
  161. idx = mipsxx_pmu_swizzle_perf_idx(idx);
  162. switch (idx) {
  163. case 0:
  164. /*
  165. * The counters are unsigned, we must cast to truncate
  166. * off the high bits.
  167. */
  168. return (u32)read_c0_perfcntr0();
  169. case 1:
  170. return (u32)read_c0_perfcntr1();
  171. case 2:
  172. return (u32)read_c0_perfcntr2();
  173. case 3:
  174. return (u32)read_c0_perfcntr3();
  175. default:
  176. WARN_ONCE(1, "Invalid performance counter number (%d)\n", idx);
  177. return 0;
  178. }
  179. }
  180. static u64 mipsxx_pmu_read_counter_64(unsigned int idx)
  181. {
  182. idx = mipsxx_pmu_swizzle_perf_idx(idx);
  183. switch (idx) {
  184. case 0:
  185. return read_c0_perfcntr0_64();
  186. case 1:
  187. return read_c0_perfcntr1_64();
  188. case 2:
  189. return read_c0_perfcntr2_64();
  190. case 3:
  191. return read_c0_perfcntr3_64();
  192. default:
  193. WARN_ONCE(1, "Invalid performance counter number (%d)\n", idx);
  194. return 0;
  195. }
  196. }
  197. static void mipsxx_pmu_write_counter(unsigned int idx, u64 val)
  198. {
  199. idx = mipsxx_pmu_swizzle_perf_idx(idx);
  200. switch (idx) {
  201. case 0:
  202. write_c0_perfcntr0(val);
  203. return;
  204. case 1:
  205. write_c0_perfcntr1(val);
  206. return;
  207. case 2:
  208. write_c0_perfcntr2(val);
  209. return;
  210. case 3:
  211. write_c0_perfcntr3(val);
  212. return;
  213. }
  214. }
  215. static void mipsxx_pmu_write_counter_64(unsigned int idx, u64 val)
  216. {
  217. idx = mipsxx_pmu_swizzle_perf_idx(idx);
  218. switch (idx) {
  219. case 0:
  220. write_c0_perfcntr0_64(val);
  221. return;
  222. case 1:
  223. write_c0_perfcntr1_64(val);
  224. return;
  225. case 2:
  226. write_c0_perfcntr2_64(val);
  227. return;
  228. case 3:
  229. write_c0_perfcntr3_64(val);
  230. return;
  231. }
  232. }
  233. static unsigned int mipsxx_pmu_read_control(unsigned int idx)
  234. {
  235. idx = mipsxx_pmu_swizzle_perf_idx(idx);
  236. switch (idx) {
  237. case 0:
  238. return read_c0_perfctrl0();
  239. case 1:
  240. return read_c0_perfctrl1();
  241. case 2:
  242. return read_c0_perfctrl2();
  243. case 3:
  244. return read_c0_perfctrl3();
  245. default:
  246. WARN_ONCE(1, "Invalid performance counter number (%d)\n", idx);
  247. return 0;
  248. }
  249. }
  250. static void mipsxx_pmu_write_control(unsigned int idx, unsigned int val)
  251. {
  252. idx = mipsxx_pmu_swizzle_perf_idx(idx);
  253. switch (idx) {
  254. case 0:
  255. write_c0_perfctrl0(val);
  256. return;
  257. case 1:
  258. write_c0_perfctrl1(val);
  259. return;
  260. case 2:
  261. write_c0_perfctrl2(val);
  262. return;
  263. case 3:
  264. write_c0_perfctrl3(val);
  265. return;
  266. }
  267. }
  268. static int mipsxx_pmu_alloc_counter(struct cpu_hw_events *cpuc,
  269. struct hw_perf_event *hwc)
  270. {
  271. int i;
  272. /*
  273. * We only need to care the counter mask. The range has been
  274. * checked definitely.
  275. */
  276. unsigned long cntr_mask = (hwc->event_base >> 8) & 0xffff;
  277. for (i = mipspmu.num_counters - 1; i >= 0; i--) {
  278. /*
  279. * Note that some MIPS perf events can be counted by both
  280. * even and odd counters, wheresas many other are only by
  281. * even _or_ odd counters. This introduces an issue that
  282. * when the former kind of event takes the counter the
  283. * latter kind of event wants to use, then the "counter
  284. * allocation" for the latter event will fail. In fact if
  285. * they can be dynamically swapped, they both feel happy.
  286. * But here we leave this issue alone for now.
  287. */
  288. if (test_bit(i, &cntr_mask) &&
  289. !test_and_set_bit(i, cpuc->used_mask))
  290. return i;
  291. }
  292. return -EAGAIN;
  293. }
  294. static void mipsxx_pmu_enable_event(struct hw_perf_event *evt, int idx)
  295. {
  296. struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
  297. WARN_ON(idx < 0 || idx >= mipspmu.num_counters);
  298. cpuc->saved_ctrl[idx] = M_PERFCTL_EVENT(evt->event_base & 0xff) |
  299. (evt->config_base & M_PERFCTL_CONFIG_MASK) |
  300. /* Make sure interrupt enabled. */
  301. M_PERFCTL_INTERRUPT_ENABLE;
  302. /*
  303. * We do not actually let the counter run. Leave it until start().
  304. */
  305. }
  306. static void mipsxx_pmu_disable_event(int idx)
  307. {
  308. struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
  309. unsigned long flags;
  310. WARN_ON(idx < 0 || idx >= mipspmu.num_counters);
  311. local_irq_save(flags);
  312. cpuc->saved_ctrl[idx] = mipsxx_pmu_read_control(idx) &
  313. ~M_PERFCTL_COUNT_EVENT_WHENEVER;
  314. mipsxx_pmu_write_control(idx, cpuc->saved_ctrl[idx]);
  315. local_irq_restore(flags);
  316. }
  317. static int mipspmu_event_set_period(struct perf_event *event,
  318. struct hw_perf_event *hwc,
  319. int idx)
  320. {
  321. u64 left = local64_read(&hwc->period_left);
  322. u64 period = hwc->sample_period;
  323. int ret = 0;
  324. if (unlikely((left + period) & (1ULL << 63))) {
  325. /* left underflowed by more than period. */
  326. left = period;
  327. local64_set(&hwc->period_left, left);
  328. hwc->last_period = period;
  329. ret = 1;
  330. } else if (unlikely((left + period) <= period)) {
  331. /* left underflowed by less than period. */
  332. left += period;
  333. local64_set(&hwc->period_left, left);
  334. hwc->last_period = period;
  335. ret = 1;
  336. }
  337. if (left > mipspmu.max_period) {
  338. left = mipspmu.max_period;
  339. local64_set(&hwc->period_left, left);
  340. }
  341. local64_set(&hwc->prev_count, mipspmu.overflow - left);
  342. mipspmu.write_counter(idx, mipspmu.overflow - left);
  343. perf_event_update_userpage(event);
  344. return ret;
  345. }
  346. static void mipspmu_event_update(struct perf_event *event,
  347. struct hw_perf_event *hwc,
  348. int idx)
  349. {
  350. u64 prev_raw_count, new_raw_count;
  351. u64 delta;
  352. again:
  353. prev_raw_count = local64_read(&hwc->prev_count);
  354. new_raw_count = mipspmu.read_counter(idx);
  355. if (local64_cmpxchg(&hwc->prev_count, prev_raw_count,
  356. new_raw_count) != prev_raw_count)
  357. goto again;
  358. delta = new_raw_count - prev_raw_count;
  359. local64_add(delta, &event->count);
  360. local64_sub(delta, &hwc->period_left);
  361. }
  362. static void mipspmu_start(struct perf_event *event, int flags)
  363. {
  364. struct hw_perf_event *hwc = &event->hw;
  365. if (flags & PERF_EF_RELOAD)
  366. WARN_ON_ONCE(!(hwc->state & PERF_HES_UPTODATE));
  367. hwc->state = 0;
  368. /* Set the period for the event. */
  369. mipspmu_event_set_period(event, hwc, hwc->idx);
  370. /* Enable the event. */
  371. mipsxx_pmu_enable_event(hwc, hwc->idx);
  372. }
  373. static void mipspmu_stop(struct perf_event *event, int flags)
  374. {
  375. struct hw_perf_event *hwc = &event->hw;
  376. if (!(hwc->state & PERF_HES_STOPPED)) {
  377. /* We are working on a local event. */
  378. mipsxx_pmu_disable_event(hwc->idx);
  379. barrier();
  380. mipspmu_event_update(event, hwc, hwc->idx);
  381. hwc->state |= PERF_HES_STOPPED | PERF_HES_UPTODATE;
  382. }
  383. }
  384. static int mipspmu_add(struct perf_event *event, int flags)
  385. {
  386. struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
  387. struct hw_perf_event *hwc = &event->hw;
  388. int idx;
  389. int err = 0;
  390. perf_pmu_disable(event->pmu);
  391. /* To look for a free counter for this event. */
  392. idx = mipsxx_pmu_alloc_counter(cpuc, hwc);
  393. if (idx < 0) {
  394. err = idx;
  395. goto out;
  396. }
  397. /*
  398. * If there is an event in the counter we are going to use then
  399. * make sure it is disabled.
  400. */
  401. event->hw.idx = idx;
  402. mipsxx_pmu_disable_event(idx);
  403. cpuc->events[idx] = event;
  404. hwc->state = PERF_HES_STOPPED | PERF_HES_UPTODATE;
  405. if (flags & PERF_EF_START)
  406. mipspmu_start(event, PERF_EF_RELOAD);
  407. /* Propagate our changes to the userspace mapping. */
  408. perf_event_update_userpage(event);
  409. out:
  410. perf_pmu_enable(event->pmu);
  411. return err;
  412. }
  413. static void mipspmu_del(struct perf_event *event, int flags)
  414. {
  415. struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
  416. struct hw_perf_event *hwc = &event->hw;
  417. int idx = hwc->idx;
  418. WARN_ON(idx < 0 || idx >= mipspmu.num_counters);
  419. mipspmu_stop(event, PERF_EF_UPDATE);
  420. cpuc->events[idx] = NULL;
  421. clear_bit(idx, cpuc->used_mask);
  422. perf_event_update_userpage(event);
  423. }
  424. static void mipspmu_read(struct perf_event *event)
  425. {
  426. struct hw_perf_event *hwc = &event->hw;
  427. /* Don't read disabled counters! */
  428. if (hwc->idx < 0)
  429. return;
  430. mipspmu_event_update(event, hwc, hwc->idx);
  431. }
  432. static void mipspmu_enable(struct pmu *pmu)
  433. {
  434. #ifdef CONFIG_MIPS_MT_SMP
  435. write_unlock(&pmuint_rwlock);
  436. #endif
  437. resume_local_counters();
  438. }
  439. /*
  440. * MIPS performance counters can be per-TC. The control registers can
  441. * not be directly accessed accross CPUs. Hence if we want to do global
  442. * control, we need cross CPU calls. on_each_cpu() can help us, but we
  443. * can not make sure this function is called with interrupts enabled. So
  444. * here we pause local counters and then grab a rwlock and leave the
  445. * counters on other CPUs alone. If any counter interrupt raises while
  446. * we own the write lock, simply pause local counters on that CPU and
  447. * spin in the handler. Also we know we won't be switched to another
  448. * CPU after pausing local counters and before grabbing the lock.
  449. */
  450. static void mipspmu_disable(struct pmu *pmu)
  451. {
  452. pause_local_counters();
  453. #ifdef CONFIG_MIPS_MT_SMP
  454. write_lock(&pmuint_rwlock);
  455. #endif
  456. }
  457. static atomic_t active_events = ATOMIC_INIT(0);
  458. static DEFINE_MUTEX(pmu_reserve_mutex);
  459. static int (*save_perf_irq)(void);
  460. static int mipspmu_get_irq(void)
  461. {
  462. int err;
  463. if (mipspmu.irq >= 0) {
  464. /* Request my own irq handler. */
  465. err = request_irq(mipspmu.irq, mipsxx_pmu_handle_irq,
  466. IRQF_PERCPU | IRQF_NOBALANCING,
  467. "mips_perf_pmu", NULL);
  468. if (err) {
  469. pr_warning("Unable to request IRQ%d for MIPS "
  470. "performance counters!\n", mipspmu.irq);
  471. }
  472. } else if (cp0_perfcount_irq < 0) {
  473. /*
  474. * We are sharing the irq number with the timer interrupt.
  475. */
  476. save_perf_irq = perf_irq;
  477. perf_irq = mipsxx_pmu_handle_shared_irq;
  478. err = 0;
  479. } else {
  480. pr_warning("The platform hasn't properly defined its "
  481. "interrupt controller.\n");
  482. err = -ENOENT;
  483. }
  484. return err;
  485. }
  486. static void mipspmu_free_irq(void)
  487. {
  488. if (mipspmu.irq >= 0)
  489. free_irq(mipspmu.irq, NULL);
  490. else if (cp0_perfcount_irq < 0)
  491. perf_irq = save_perf_irq;
  492. }
  493. /*
  494. * mipsxx/rm9000/loongson2 have different performance counters, they have
  495. * specific low-level init routines.
  496. */
  497. static void reset_counters(void *arg);
  498. static int __hw_perf_event_init(struct perf_event *event);
  499. static void hw_perf_event_destroy(struct perf_event *event)
  500. {
  501. if (atomic_dec_and_mutex_lock(&active_events,
  502. &pmu_reserve_mutex)) {
  503. /*
  504. * We must not call the destroy function with interrupts
  505. * disabled.
  506. */
  507. on_each_cpu(reset_counters,
  508. (void *)(long)mipspmu.num_counters, 1);
  509. mipspmu_free_irq();
  510. mutex_unlock(&pmu_reserve_mutex);
  511. }
  512. }
  513. static int mipspmu_event_init(struct perf_event *event)
  514. {
  515. int err = 0;
  516. /* does not support taken branch sampling */
  517. if (has_branch_stack(event))
  518. return -EOPNOTSUPP;
  519. switch (event->attr.type) {
  520. case PERF_TYPE_RAW:
  521. case PERF_TYPE_HARDWARE:
  522. case PERF_TYPE_HW_CACHE:
  523. break;
  524. default:
  525. return -ENOENT;
  526. }
  527. if (event->cpu >= nr_cpumask_bits ||
  528. (event->cpu >= 0 && !cpu_online(event->cpu)))
  529. return -ENODEV;
  530. if (!atomic_inc_not_zero(&active_events)) {
  531. mutex_lock(&pmu_reserve_mutex);
  532. if (atomic_read(&active_events) == 0)
  533. err = mipspmu_get_irq();
  534. if (!err)
  535. atomic_inc(&active_events);
  536. mutex_unlock(&pmu_reserve_mutex);
  537. }
  538. if (err)
  539. return err;
  540. return __hw_perf_event_init(event);
  541. }
  542. static struct pmu pmu = {
  543. .pmu_enable = mipspmu_enable,
  544. .pmu_disable = mipspmu_disable,
  545. .event_init = mipspmu_event_init,
  546. .add = mipspmu_add,
  547. .del = mipspmu_del,
  548. .start = mipspmu_start,
  549. .stop = mipspmu_stop,
  550. .read = mipspmu_read,
  551. };
  552. static unsigned int mipspmu_perf_event_encode(const struct mips_perf_event *pev)
  553. {
  554. /*
  555. * Top 8 bits for range, next 16 bits for cntr_mask, lowest 8 bits for
  556. * event_id.
  557. */
  558. #ifdef CONFIG_MIPS_MT_SMP
  559. return ((unsigned int)pev->range << 24) |
  560. (pev->cntr_mask & 0xffff00) |
  561. (pev->event_id & 0xff);
  562. #else
  563. return (pev->cntr_mask & 0xffff00) |
  564. (pev->event_id & 0xff);
  565. #endif
  566. }
  567. static const struct mips_perf_event *mipspmu_map_general_event(int idx)
  568. {
  569. const struct mips_perf_event *pev;
  570. pev = ((*mipspmu.general_event_map)[idx].event_id ==
  571. UNSUPPORTED_PERF_EVENT_ID ? ERR_PTR(-EOPNOTSUPP) :
  572. &(*mipspmu.general_event_map)[idx]);
  573. return pev;
  574. }
  575. static const struct mips_perf_event *mipspmu_map_cache_event(u64 config)
  576. {
  577. unsigned int cache_type, cache_op, cache_result;
  578. const struct mips_perf_event *pev;
  579. cache_type = (config >> 0) & 0xff;
  580. if (cache_type >= PERF_COUNT_HW_CACHE_MAX)
  581. return ERR_PTR(-EINVAL);
  582. cache_op = (config >> 8) & 0xff;
  583. if (cache_op >= PERF_COUNT_HW_CACHE_OP_MAX)
  584. return ERR_PTR(-EINVAL);
  585. cache_result = (config >> 16) & 0xff;
  586. if (cache_result >= PERF_COUNT_HW_CACHE_RESULT_MAX)
  587. return ERR_PTR(-EINVAL);
  588. pev = &((*mipspmu.cache_event_map)
  589. [cache_type]
  590. [cache_op]
  591. [cache_result]);
  592. if (pev->event_id == UNSUPPORTED_PERF_EVENT_ID)
  593. return ERR_PTR(-EOPNOTSUPP);
  594. return pev;
  595. }
  596. static int validate_group(struct perf_event *event)
  597. {
  598. struct perf_event *sibling, *leader = event->group_leader;
  599. struct cpu_hw_events fake_cpuc;
  600. memset(&fake_cpuc, 0, sizeof(fake_cpuc));
  601. if (mipsxx_pmu_alloc_counter(&fake_cpuc, &leader->hw) < 0)
  602. return -EINVAL;
  603. list_for_each_entry(sibling, &leader->sibling_list, group_entry) {
  604. if (mipsxx_pmu_alloc_counter(&fake_cpuc, &sibling->hw) < 0)
  605. return -EINVAL;
  606. }
  607. if (mipsxx_pmu_alloc_counter(&fake_cpuc, &event->hw) < 0)
  608. return -EINVAL;
  609. return 0;
  610. }
  611. /* This is needed by specific irq handlers in perf_event_*.c */
  612. static void handle_associated_event(struct cpu_hw_events *cpuc,
  613. int idx, struct perf_sample_data *data,
  614. struct pt_regs *regs)
  615. {
  616. struct perf_event *event = cpuc->events[idx];
  617. struct hw_perf_event *hwc = &event->hw;
  618. mipspmu_event_update(event, hwc, idx);
  619. data->period = event->hw.last_period;
  620. if (!mipspmu_event_set_period(event, hwc, idx))
  621. return;
  622. if (perf_event_overflow(event, data, regs))
  623. mipsxx_pmu_disable_event(idx);
  624. }
  625. static int __n_counters(void)
  626. {
  627. if (!(read_c0_config1() & M_CONFIG1_PC))
  628. return 0;
  629. if (!(read_c0_perfctrl0() & M_PERFCTL_MORE))
  630. return 1;
  631. if (!(read_c0_perfctrl1() & M_PERFCTL_MORE))
  632. return 2;
  633. if (!(read_c0_perfctrl2() & M_PERFCTL_MORE))
  634. return 3;
  635. return 4;
  636. }
  637. static int n_counters(void)
  638. {
  639. int counters;
  640. switch (current_cpu_type()) {
  641. case CPU_R10000:
  642. counters = 2;
  643. break;
  644. case CPU_R12000:
  645. case CPU_R14000:
  646. counters = 4;
  647. break;
  648. default:
  649. counters = __n_counters();
  650. }
  651. return counters;
  652. }
  653. static void reset_counters(void *arg)
  654. {
  655. int counters = (int)(long)arg;
  656. switch (counters) {
  657. case 4:
  658. mipsxx_pmu_write_control(3, 0);
  659. mipspmu.write_counter(3, 0);
  660. case 3:
  661. mipsxx_pmu_write_control(2, 0);
  662. mipspmu.write_counter(2, 0);
  663. case 2:
  664. mipsxx_pmu_write_control(1, 0);
  665. mipspmu.write_counter(1, 0);
  666. case 1:
  667. mipsxx_pmu_write_control(0, 0);
  668. mipspmu.write_counter(0, 0);
  669. }
  670. }
  671. /* 24K/34K/1004K cores can share the same event map. */
  672. static const struct mips_perf_event mipsxxcore_event_map
  673. [PERF_COUNT_HW_MAX] = {
  674. [PERF_COUNT_HW_CPU_CYCLES] = { 0x00, CNTR_EVEN | CNTR_ODD, P },
  675. [PERF_COUNT_HW_INSTRUCTIONS] = { 0x01, CNTR_EVEN | CNTR_ODD, T },
  676. [PERF_COUNT_HW_CACHE_REFERENCES] = { UNSUPPORTED_PERF_EVENT_ID },
  677. [PERF_COUNT_HW_CACHE_MISSES] = { UNSUPPORTED_PERF_EVENT_ID },
  678. [PERF_COUNT_HW_BRANCH_INSTRUCTIONS] = { 0x02, CNTR_EVEN, T },
  679. [PERF_COUNT_HW_BRANCH_MISSES] = { 0x02, CNTR_ODD, T },
  680. [PERF_COUNT_HW_BUS_CYCLES] = { UNSUPPORTED_PERF_EVENT_ID },
  681. };
  682. /* 74K core has different branch event code. */
  683. static const struct mips_perf_event mipsxx74Kcore_event_map
  684. [PERF_COUNT_HW_MAX] = {
  685. [PERF_COUNT_HW_CPU_CYCLES] = { 0x00, CNTR_EVEN | CNTR_ODD, P },
  686. [PERF_COUNT_HW_INSTRUCTIONS] = { 0x01, CNTR_EVEN | CNTR_ODD, T },
  687. [PERF_COUNT_HW_CACHE_REFERENCES] = { UNSUPPORTED_PERF_EVENT_ID },
  688. [PERF_COUNT_HW_CACHE_MISSES] = { UNSUPPORTED_PERF_EVENT_ID },
  689. [PERF_COUNT_HW_BRANCH_INSTRUCTIONS] = { 0x27, CNTR_EVEN, T },
  690. [PERF_COUNT_HW_BRANCH_MISSES] = { 0x27, CNTR_ODD, T },
  691. [PERF_COUNT_HW_BUS_CYCLES] = { UNSUPPORTED_PERF_EVENT_ID },
  692. };
  693. static const struct mips_perf_event octeon_event_map[PERF_COUNT_HW_MAX] = {
  694. [PERF_COUNT_HW_CPU_CYCLES] = { 0x01, CNTR_ALL },
  695. [PERF_COUNT_HW_INSTRUCTIONS] = { 0x03, CNTR_ALL },
  696. [PERF_COUNT_HW_CACHE_REFERENCES] = { 0x2b, CNTR_ALL },
  697. [PERF_COUNT_HW_CACHE_MISSES] = { 0x2e, CNTR_ALL },
  698. [PERF_COUNT_HW_BRANCH_INSTRUCTIONS] = { 0x08, CNTR_ALL },
  699. [PERF_COUNT_HW_BRANCH_MISSES] = { 0x09, CNTR_ALL },
  700. [PERF_COUNT_HW_BUS_CYCLES] = { 0x25, CNTR_ALL },
  701. };
  702. /* 24K/34K/1004K cores can share the same cache event map. */
  703. static const struct mips_perf_event mipsxxcore_cache_map
  704. [PERF_COUNT_HW_CACHE_MAX]
  705. [PERF_COUNT_HW_CACHE_OP_MAX]
  706. [PERF_COUNT_HW_CACHE_RESULT_MAX] = {
  707. [C(L1D)] = {
  708. /*
  709. * Like some other architectures (e.g. ARM), the performance
  710. * counters don't differentiate between read and write
  711. * accesses/misses, so this isn't strictly correct, but it's the
  712. * best we can do. Writes and reads get combined.
  713. */
  714. [C(OP_READ)] = {
  715. [C(RESULT_ACCESS)] = { 0x0a, CNTR_EVEN, T },
  716. [C(RESULT_MISS)] = { 0x0b, CNTR_EVEN | CNTR_ODD, T },
  717. },
  718. [C(OP_WRITE)] = {
  719. [C(RESULT_ACCESS)] = { 0x0a, CNTR_EVEN, T },
  720. [C(RESULT_MISS)] = { 0x0b, CNTR_EVEN | CNTR_ODD, T },
  721. },
  722. [C(OP_PREFETCH)] = {
  723. [C(RESULT_ACCESS)] = { UNSUPPORTED_PERF_EVENT_ID },
  724. [C(RESULT_MISS)] = { UNSUPPORTED_PERF_EVENT_ID },
  725. },
  726. },
  727. [C(L1I)] = {
  728. [C(OP_READ)] = {
  729. [C(RESULT_ACCESS)] = { 0x09, CNTR_EVEN, T },
  730. [C(RESULT_MISS)] = { 0x09, CNTR_ODD, T },
  731. },
  732. [C(OP_WRITE)] = {
  733. [C(RESULT_ACCESS)] = { 0x09, CNTR_EVEN, T },
  734. [C(RESULT_MISS)] = { 0x09, CNTR_ODD, T },
  735. },
  736. [C(OP_PREFETCH)] = {
  737. [C(RESULT_ACCESS)] = { 0x14, CNTR_EVEN, T },
  738. /*
  739. * Note that MIPS has only "hit" events countable for
  740. * the prefetch operation.
  741. */
  742. [C(RESULT_MISS)] = { UNSUPPORTED_PERF_EVENT_ID },
  743. },
  744. },
  745. [C(LL)] = {
  746. [C(OP_READ)] = {
  747. [C(RESULT_ACCESS)] = { 0x15, CNTR_ODD, P },
  748. [C(RESULT_MISS)] = { 0x16, CNTR_EVEN, P },
  749. },
  750. [C(OP_WRITE)] = {
  751. [C(RESULT_ACCESS)] = { 0x15, CNTR_ODD, P },
  752. [C(RESULT_MISS)] = { 0x16, CNTR_EVEN, P },
  753. },
  754. [C(OP_PREFETCH)] = {
  755. [C(RESULT_ACCESS)] = { UNSUPPORTED_PERF_EVENT_ID },
  756. [C(RESULT_MISS)] = { UNSUPPORTED_PERF_EVENT_ID },
  757. },
  758. },
  759. [C(DTLB)] = {
  760. [C(OP_READ)] = {
  761. [C(RESULT_ACCESS)] = { 0x06, CNTR_EVEN, T },
  762. [C(RESULT_MISS)] = { 0x06, CNTR_ODD, T },
  763. },
  764. [C(OP_WRITE)] = {
  765. [C(RESULT_ACCESS)] = { 0x06, CNTR_EVEN, T },
  766. [C(RESULT_MISS)] = { 0x06, CNTR_ODD, T },
  767. },
  768. [C(OP_PREFETCH)] = {
  769. [C(RESULT_ACCESS)] = { UNSUPPORTED_PERF_EVENT_ID },
  770. [C(RESULT_MISS)] = { UNSUPPORTED_PERF_EVENT_ID },
  771. },
  772. },
  773. [C(ITLB)] = {
  774. [C(OP_READ)] = {
  775. [C(RESULT_ACCESS)] = { 0x05, CNTR_EVEN, T },
  776. [C(RESULT_MISS)] = { 0x05, CNTR_ODD, T },
  777. },
  778. [C(OP_WRITE)] = {
  779. [C(RESULT_ACCESS)] = { 0x05, CNTR_EVEN, T },
  780. [C(RESULT_MISS)] = { 0x05, CNTR_ODD, T },
  781. },
  782. [C(OP_PREFETCH)] = {
  783. [C(RESULT_ACCESS)] = { UNSUPPORTED_PERF_EVENT_ID },
  784. [C(RESULT_MISS)] = { UNSUPPORTED_PERF_EVENT_ID },
  785. },
  786. },
  787. [C(BPU)] = {
  788. /* Using the same code for *HW_BRANCH* */
  789. [C(OP_READ)] = {
  790. [C(RESULT_ACCESS)] = { 0x02, CNTR_EVEN, T },
  791. [C(RESULT_MISS)] = { 0x02, CNTR_ODD, T },
  792. },
  793. [C(OP_WRITE)] = {
  794. [C(RESULT_ACCESS)] = { 0x02, CNTR_EVEN, T },
  795. [C(RESULT_MISS)] = { 0x02, CNTR_ODD, T },
  796. },
  797. [C(OP_PREFETCH)] = {
  798. [C(RESULT_ACCESS)] = { UNSUPPORTED_PERF_EVENT_ID },
  799. [C(RESULT_MISS)] = { UNSUPPORTED_PERF_EVENT_ID },
  800. },
  801. },
  802. [C(NODE)] = {
  803. [C(OP_READ)] = {
  804. [C(RESULT_ACCESS)] = { UNSUPPORTED_PERF_EVENT_ID },
  805. [C(RESULT_MISS)] = { UNSUPPORTED_PERF_EVENT_ID },
  806. },
  807. [C(OP_WRITE)] = {
  808. [C(RESULT_ACCESS)] = { UNSUPPORTED_PERF_EVENT_ID },
  809. [C(RESULT_MISS)] = { UNSUPPORTED_PERF_EVENT_ID },
  810. },
  811. [C(OP_PREFETCH)] = {
  812. [C(RESULT_ACCESS)] = { UNSUPPORTED_PERF_EVENT_ID },
  813. [C(RESULT_MISS)] = { UNSUPPORTED_PERF_EVENT_ID },
  814. },
  815. },
  816. };
  817. /* 74K core has completely different cache event map. */
  818. static const struct mips_perf_event mipsxx74Kcore_cache_map
  819. [PERF_COUNT_HW_CACHE_MAX]
  820. [PERF_COUNT_HW_CACHE_OP_MAX]
  821. [PERF_COUNT_HW_CACHE_RESULT_MAX] = {
  822. [C(L1D)] = {
  823. /*
  824. * Like some other architectures (e.g. ARM), the performance
  825. * counters don't differentiate between read and write
  826. * accesses/misses, so this isn't strictly correct, but it's the
  827. * best we can do. Writes and reads get combined.
  828. */
  829. [C(OP_READ)] = {
  830. [C(RESULT_ACCESS)] = { 0x17, CNTR_ODD, T },
  831. [C(RESULT_MISS)] = { 0x18, CNTR_ODD, T },
  832. },
  833. [C(OP_WRITE)] = {
  834. [C(RESULT_ACCESS)] = { 0x17, CNTR_ODD, T },
  835. [C(RESULT_MISS)] = { 0x18, CNTR_ODD, T },
  836. },
  837. [C(OP_PREFETCH)] = {
  838. [C(RESULT_ACCESS)] = { UNSUPPORTED_PERF_EVENT_ID },
  839. [C(RESULT_MISS)] = { UNSUPPORTED_PERF_EVENT_ID },
  840. },
  841. },
  842. [C(L1I)] = {
  843. [C(OP_READ)] = {
  844. [C(RESULT_ACCESS)] = { 0x06, CNTR_EVEN, T },
  845. [C(RESULT_MISS)] = { 0x06, CNTR_ODD, T },
  846. },
  847. [C(OP_WRITE)] = {
  848. [C(RESULT_ACCESS)] = { 0x06, CNTR_EVEN, T },
  849. [C(RESULT_MISS)] = { 0x06, CNTR_ODD, T },
  850. },
  851. [C(OP_PREFETCH)] = {
  852. [C(RESULT_ACCESS)] = { 0x34, CNTR_EVEN, T },
  853. /*
  854. * Note that MIPS has only "hit" events countable for
  855. * the prefetch operation.
  856. */
  857. [C(RESULT_MISS)] = { UNSUPPORTED_PERF_EVENT_ID },
  858. },
  859. },
  860. [C(LL)] = {
  861. [C(OP_READ)] = {
  862. [C(RESULT_ACCESS)] = { 0x1c, CNTR_ODD, P },
  863. [C(RESULT_MISS)] = { 0x1d, CNTR_EVEN | CNTR_ODD, P },
  864. },
  865. [C(OP_WRITE)] = {
  866. [C(RESULT_ACCESS)] = { 0x1c, CNTR_ODD, P },
  867. [C(RESULT_MISS)] = { 0x1d, CNTR_EVEN | CNTR_ODD, P },
  868. },
  869. [C(OP_PREFETCH)] = {
  870. [C(RESULT_ACCESS)] = { UNSUPPORTED_PERF_EVENT_ID },
  871. [C(RESULT_MISS)] = { UNSUPPORTED_PERF_EVENT_ID },
  872. },
  873. },
  874. [C(DTLB)] = {
  875. /* 74K core does not have specific DTLB events. */
  876. [C(OP_READ)] = {
  877. [C(RESULT_ACCESS)] = { UNSUPPORTED_PERF_EVENT_ID },
  878. [C(RESULT_MISS)] = { UNSUPPORTED_PERF_EVENT_ID },
  879. },
  880. [C(OP_WRITE)] = {
  881. [C(RESULT_ACCESS)] = { UNSUPPORTED_PERF_EVENT_ID },
  882. [C(RESULT_MISS)] = { UNSUPPORTED_PERF_EVENT_ID },
  883. },
  884. [C(OP_PREFETCH)] = {
  885. [C(RESULT_ACCESS)] = { UNSUPPORTED_PERF_EVENT_ID },
  886. [C(RESULT_MISS)] = { UNSUPPORTED_PERF_EVENT_ID },
  887. },
  888. },
  889. [C(ITLB)] = {
  890. [C(OP_READ)] = {
  891. [C(RESULT_ACCESS)] = { 0x04, CNTR_EVEN, T },
  892. [C(RESULT_MISS)] = { 0x04, CNTR_ODD, T },
  893. },
  894. [C(OP_WRITE)] = {
  895. [C(RESULT_ACCESS)] = { 0x04, CNTR_EVEN, T },
  896. [C(RESULT_MISS)] = { 0x04, CNTR_ODD, T },
  897. },
  898. [C(OP_PREFETCH)] = {
  899. [C(RESULT_ACCESS)] = { UNSUPPORTED_PERF_EVENT_ID },
  900. [C(RESULT_MISS)] = { UNSUPPORTED_PERF_EVENT_ID },
  901. },
  902. },
  903. [C(BPU)] = {
  904. /* Using the same code for *HW_BRANCH* */
  905. [C(OP_READ)] = {
  906. [C(RESULT_ACCESS)] = { 0x27, CNTR_EVEN, T },
  907. [C(RESULT_MISS)] = { 0x27, CNTR_ODD, T },
  908. },
  909. [C(OP_WRITE)] = {
  910. [C(RESULT_ACCESS)] = { 0x27, CNTR_EVEN, T },
  911. [C(RESULT_MISS)] = { 0x27, CNTR_ODD, T },
  912. },
  913. [C(OP_PREFETCH)] = {
  914. [C(RESULT_ACCESS)] = { UNSUPPORTED_PERF_EVENT_ID },
  915. [C(RESULT_MISS)] = { UNSUPPORTED_PERF_EVENT_ID },
  916. },
  917. },
  918. [C(NODE)] = {
  919. [C(OP_READ)] = {
  920. [C(RESULT_ACCESS)] = { UNSUPPORTED_PERF_EVENT_ID },
  921. [C(RESULT_MISS)] = { UNSUPPORTED_PERF_EVENT_ID },
  922. },
  923. [C(OP_WRITE)] = {
  924. [C(RESULT_ACCESS)] = { UNSUPPORTED_PERF_EVENT_ID },
  925. [C(RESULT_MISS)] = { UNSUPPORTED_PERF_EVENT_ID },
  926. },
  927. [C(OP_PREFETCH)] = {
  928. [C(RESULT_ACCESS)] = { UNSUPPORTED_PERF_EVENT_ID },
  929. [C(RESULT_MISS)] = { UNSUPPORTED_PERF_EVENT_ID },
  930. },
  931. },
  932. };
  933. static const struct mips_perf_event octeon_cache_map
  934. [PERF_COUNT_HW_CACHE_MAX]
  935. [PERF_COUNT_HW_CACHE_OP_MAX]
  936. [PERF_COUNT_HW_CACHE_RESULT_MAX] = {
  937. [C(L1D)] = {
  938. [C(OP_READ)] = {
  939. [C(RESULT_ACCESS)] = { 0x2b, CNTR_ALL },
  940. [C(RESULT_MISS)] = { 0x2e, CNTR_ALL },
  941. },
  942. [C(OP_WRITE)] = {
  943. [C(RESULT_ACCESS)] = { 0x30, CNTR_ALL },
  944. [C(RESULT_MISS)] = { UNSUPPORTED_PERF_EVENT_ID },
  945. },
  946. [C(OP_PREFETCH)] = {
  947. [C(RESULT_ACCESS)] = { UNSUPPORTED_PERF_EVENT_ID },
  948. [C(RESULT_MISS)] = { UNSUPPORTED_PERF_EVENT_ID },
  949. },
  950. },
  951. [C(L1I)] = {
  952. [C(OP_READ)] = {
  953. [C(RESULT_ACCESS)] = { 0x18, CNTR_ALL },
  954. [C(RESULT_MISS)] = { UNSUPPORTED_PERF_EVENT_ID },
  955. },
  956. [C(OP_WRITE)] = {
  957. [C(RESULT_ACCESS)] = { UNSUPPORTED_PERF_EVENT_ID },
  958. [C(RESULT_MISS)] = { UNSUPPORTED_PERF_EVENT_ID },
  959. },
  960. [C(OP_PREFETCH)] = {
  961. [C(RESULT_ACCESS)] = { 0x19, CNTR_ALL },
  962. [C(RESULT_MISS)] = { UNSUPPORTED_PERF_EVENT_ID },
  963. },
  964. },
  965. [C(LL)] = {
  966. [C(OP_READ)] = {
  967. [C(RESULT_ACCESS)] = { UNSUPPORTED_PERF_EVENT_ID },
  968. [C(RESULT_MISS)] = { UNSUPPORTED_PERF_EVENT_ID },
  969. },
  970. [C(OP_WRITE)] = {
  971. [C(RESULT_ACCESS)] = { UNSUPPORTED_PERF_EVENT_ID },
  972. [C(RESULT_MISS)] = { UNSUPPORTED_PERF_EVENT_ID },
  973. },
  974. [C(OP_PREFETCH)] = {
  975. [C(RESULT_ACCESS)] = { UNSUPPORTED_PERF_EVENT_ID },
  976. [C(RESULT_MISS)] = { UNSUPPORTED_PERF_EVENT_ID },
  977. },
  978. },
  979. [C(DTLB)] = {
  980. /*
  981. * Only general DTLB misses are counted use the same event for
  982. * read and write.
  983. */
  984. [C(OP_READ)] = {
  985. [C(RESULT_ACCESS)] = { UNSUPPORTED_PERF_EVENT_ID },
  986. [C(RESULT_MISS)] = { 0x35, CNTR_ALL },
  987. },
  988. [C(OP_WRITE)] = {
  989. [C(RESULT_ACCESS)] = { UNSUPPORTED_PERF_EVENT_ID },
  990. [C(RESULT_MISS)] = { 0x35, CNTR_ALL },
  991. },
  992. [C(OP_PREFETCH)] = {
  993. [C(RESULT_ACCESS)] = { UNSUPPORTED_PERF_EVENT_ID },
  994. [C(RESULT_MISS)] = { UNSUPPORTED_PERF_EVENT_ID },
  995. },
  996. },
  997. [C(ITLB)] = {
  998. [C(OP_READ)] = {
  999. [C(RESULT_ACCESS)] = { UNSUPPORTED_PERF_EVENT_ID },
  1000. [C(RESULT_MISS)] = { 0x37, CNTR_ALL },
  1001. },
  1002. [C(OP_WRITE)] = {
  1003. [C(RESULT_ACCESS)] = { UNSUPPORTED_PERF_EVENT_ID },
  1004. [C(RESULT_MISS)] = { UNSUPPORTED_PERF_EVENT_ID },
  1005. },
  1006. [C(OP_PREFETCH)] = {
  1007. [C(RESULT_ACCESS)] = { UNSUPPORTED_PERF_EVENT_ID },
  1008. [C(RESULT_MISS)] = { UNSUPPORTED_PERF_EVENT_ID },
  1009. },
  1010. },
  1011. [C(BPU)] = {
  1012. /* Using the same code for *HW_BRANCH* */
  1013. [C(OP_READ)] = {
  1014. [C(RESULT_ACCESS)] = { UNSUPPORTED_PERF_EVENT_ID },
  1015. [C(RESULT_MISS)] = { UNSUPPORTED_PERF_EVENT_ID },
  1016. },
  1017. [C(OP_WRITE)] = {
  1018. [C(RESULT_ACCESS)] = { UNSUPPORTED_PERF_EVENT_ID },
  1019. [C(RESULT_MISS)] = { UNSUPPORTED_PERF_EVENT_ID },
  1020. },
  1021. [C(OP_PREFETCH)] = {
  1022. [C(RESULT_ACCESS)] = { UNSUPPORTED_PERF_EVENT_ID },
  1023. [C(RESULT_MISS)] = { UNSUPPORTED_PERF_EVENT_ID },
  1024. },
  1025. },
  1026. };
  1027. #ifdef CONFIG_MIPS_MT_SMP
  1028. static void check_and_calc_range(struct perf_event *event,
  1029. const struct mips_perf_event *pev)
  1030. {
  1031. struct hw_perf_event *hwc = &event->hw;
  1032. if (event->cpu >= 0) {
  1033. if (pev->range > V) {
  1034. /*
  1035. * The user selected an event that is processor
  1036. * wide, while expecting it to be VPE wide.
  1037. */
  1038. hwc->config_base |= M_TC_EN_ALL;
  1039. } else {
  1040. /*
  1041. * FIXME: cpu_data[event->cpu].vpe_id reports 0
  1042. * for both CPUs.
  1043. */
  1044. hwc->config_base |= M_PERFCTL_VPEID(event->cpu);
  1045. hwc->config_base |= M_TC_EN_VPE;
  1046. }
  1047. } else
  1048. hwc->config_base |= M_TC_EN_ALL;
  1049. }
  1050. #else
  1051. static void check_and_calc_range(struct perf_event *event,
  1052. const struct mips_perf_event *pev)
  1053. {
  1054. }
  1055. #endif
  1056. static int __hw_perf_event_init(struct perf_event *event)
  1057. {
  1058. struct perf_event_attr *attr = &event->attr;
  1059. struct hw_perf_event *hwc = &event->hw;
  1060. const struct mips_perf_event *pev;
  1061. int err;
  1062. /* Returning MIPS event descriptor for generic perf event. */
  1063. if (PERF_TYPE_HARDWARE == event->attr.type) {
  1064. if (event->attr.config >= PERF_COUNT_HW_MAX)
  1065. return -EINVAL;
  1066. pev = mipspmu_map_general_event(event->attr.config);
  1067. } else if (PERF_TYPE_HW_CACHE == event->attr.type) {
  1068. pev = mipspmu_map_cache_event(event->attr.config);
  1069. } else if (PERF_TYPE_RAW == event->attr.type) {
  1070. /* We are working on the global raw event. */
  1071. mutex_lock(&raw_event_mutex);
  1072. pev = mipspmu.map_raw_event(event->attr.config);
  1073. } else {
  1074. /* The event type is not (yet) supported. */
  1075. return -EOPNOTSUPP;
  1076. }
  1077. if (IS_ERR(pev)) {
  1078. if (PERF_TYPE_RAW == event->attr.type)
  1079. mutex_unlock(&raw_event_mutex);
  1080. return PTR_ERR(pev);
  1081. }
  1082. /*
  1083. * We allow max flexibility on how each individual counter shared
  1084. * by the single CPU operates (the mode exclusion and the range).
  1085. */
  1086. hwc->config_base = M_PERFCTL_INTERRUPT_ENABLE;
  1087. /* Calculate range bits and validate it. */
  1088. if (num_possible_cpus() > 1)
  1089. check_and_calc_range(event, pev);
  1090. hwc->event_base = mipspmu_perf_event_encode(pev);
  1091. if (PERF_TYPE_RAW == event->attr.type)
  1092. mutex_unlock(&raw_event_mutex);
  1093. if (!attr->exclude_user)
  1094. hwc->config_base |= M_PERFCTL_USER;
  1095. if (!attr->exclude_kernel) {
  1096. hwc->config_base |= M_PERFCTL_KERNEL;
  1097. /* MIPS kernel mode: KSU == 00b || EXL == 1 || ERL == 1 */
  1098. hwc->config_base |= M_PERFCTL_EXL;
  1099. }
  1100. if (!attr->exclude_hv)
  1101. hwc->config_base |= M_PERFCTL_SUPERVISOR;
  1102. hwc->config_base &= M_PERFCTL_CONFIG_MASK;
  1103. /*
  1104. * The event can belong to another cpu. We do not assign a local
  1105. * counter for it for now.
  1106. */
  1107. hwc->idx = -1;
  1108. hwc->config = 0;
  1109. if (!hwc->sample_period) {
  1110. hwc->sample_period = mipspmu.max_period;
  1111. hwc->last_period = hwc->sample_period;
  1112. local64_set(&hwc->period_left, hwc->sample_period);
  1113. }
  1114. err = 0;
  1115. if (event->group_leader != event)
  1116. err = validate_group(event);
  1117. event->destroy = hw_perf_event_destroy;
  1118. if (err)
  1119. event->destroy(event);
  1120. return err;
  1121. }
  1122. static void pause_local_counters(void)
  1123. {
  1124. struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
  1125. int ctr = mipspmu.num_counters;
  1126. unsigned long flags;
  1127. local_irq_save(flags);
  1128. do {
  1129. ctr--;
  1130. cpuc->saved_ctrl[ctr] = mipsxx_pmu_read_control(ctr);
  1131. mipsxx_pmu_write_control(ctr, cpuc->saved_ctrl[ctr] &
  1132. ~M_PERFCTL_COUNT_EVENT_WHENEVER);
  1133. } while (ctr > 0);
  1134. local_irq_restore(flags);
  1135. }
  1136. static void resume_local_counters(void)
  1137. {
  1138. struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
  1139. int ctr = mipspmu.num_counters;
  1140. do {
  1141. ctr--;
  1142. mipsxx_pmu_write_control(ctr, cpuc->saved_ctrl[ctr]);
  1143. } while (ctr > 0);
  1144. }
  1145. static int mipsxx_pmu_handle_shared_irq(void)
  1146. {
  1147. struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
  1148. struct perf_sample_data data;
  1149. unsigned int counters = mipspmu.num_counters;
  1150. u64 counter;
  1151. int handled = IRQ_NONE;
  1152. struct pt_regs *regs;
  1153. if (cpu_has_mips_r2 && !(read_c0_cause() & (1 << 26)))
  1154. return handled;
  1155. /*
  1156. * First we pause the local counters, so that when we are locked
  1157. * here, the counters are all paused. When it gets locked due to
  1158. * perf_disable(), the timer interrupt handler will be delayed.
  1159. *
  1160. * See also mipsxx_pmu_start().
  1161. */
  1162. pause_local_counters();
  1163. #ifdef CONFIG_MIPS_MT_SMP
  1164. read_lock(&pmuint_rwlock);
  1165. #endif
  1166. regs = get_irq_regs();
  1167. perf_sample_data_init(&data, 0);
  1168. switch (counters) {
  1169. #define HANDLE_COUNTER(n) \
  1170. case n + 1: \
  1171. if (test_bit(n, cpuc->used_mask)) { \
  1172. counter = mipspmu.read_counter(n); \
  1173. if (counter & mipspmu.overflow) { \
  1174. handle_associated_event(cpuc, n, &data, regs); \
  1175. handled = IRQ_HANDLED; \
  1176. } \
  1177. }
  1178. HANDLE_COUNTER(3)
  1179. HANDLE_COUNTER(2)
  1180. HANDLE_COUNTER(1)
  1181. HANDLE_COUNTER(0)
  1182. }
  1183. /*
  1184. * Do all the work for the pending perf events. We can do this
  1185. * in here because the performance counter interrupt is a regular
  1186. * interrupt, not NMI.
  1187. */
  1188. if (handled == IRQ_HANDLED)
  1189. irq_work_run();
  1190. #ifdef CONFIG_MIPS_MT_SMP
  1191. read_unlock(&pmuint_rwlock);
  1192. #endif
  1193. resume_local_counters();
  1194. return handled;
  1195. }
  1196. static irqreturn_t mipsxx_pmu_handle_irq(int irq, void *dev)
  1197. {
  1198. return mipsxx_pmu_handle_shared_irq();
  1199. }
  1200. /* 24K */
  1201. #define IS_BOTH_COUNTERS_24K_EVENT(b) \
  1202. ((b) == 0 || (b) == 1 || (b) == 11)
  1203. /* 34K */
  1204. #define IS_BOTH_COUNTERS_34K_EVENT(b) \
  1205. ((b) == 0 || (b) == 1 || (b) == 11)
  1206. #ifdef CONFIG_MIPS_MT_SMP
  1207. #define IS_RANGE_P_34K_EVENT(r, b) \
  1208. ((b) == 0 || (r) == 18 || (b) == 21 || (b) == 22 || \
  1209. (b) == 25 || (b) == 39 || (r) == 44 || (r) == 174 || \
  1210. (r) == 176 || ((b) >= 50 && (b) <= 55) || \
  1211. ((b) >= 64 && (b) <= 67))
  1212. #define IS_RANGE_V_34K_EVENT(r) ((r) == 47)
  1213. #endif
  1214. /* 74K */
  1215. #define IS_BOTH_COUNTERS_74K_EVENT(b) \
  1216. ((b) == 0 || (b) == 1)
  1217. /* 1004K */
  1218. #define IS_BOTH_COUNTERS_1004K_EVENT(b) \
  1219. ((b) == 0 || (b) == 1 || (b) == 11)
  1220. #ifdef CONFIG_MIPS_MT_SMP
  1221. #define IS_RANGE_P_1004K_EVENT(r, b) \
  1222. ((b) == 0 || (r) == 18 || (b) == 21 || (b) == 22 || \
  1223. (b) == 25 || (b) == 36 || (b) == 39 || (r) == 44 || \
  1224. (r) == 174 || (r) == 176 || ((b) >= 50 && (b) <= 59) || \
  1225. (r) == 188 || (b) == 61 || (b) == 62 || \
  1226. ((b) >= 64 && (b) <= 67))
  1227. #define IS_RANGE_V_1004K_EVENT(r) ((r) == 47)
  1228. #endif
  1229. /*
  1230. * User can use 0-255 raw events, where 0-127 for the events of even
  1231. * counters, and 128-255 for odd counters. Note that bit 7 is used to
  1232. * indicate the parity. So, for example, when user wants to take the
  1233. * Event Num of 15 for odd counters (by referring to the user manual),
  1234. * then 128 needs to be added to 15 as the input for the event config,
  1235. * i.e., 143 (0x8F) to be used.
  1236. */
  1237. static const struct mips_perf_event *mipsxx_pmu_map_raw_event(u64 config)
  1238. {
  1239. unsigned int raw_id = config & 0xff;
  1240. unsigned int base_id = raw_id & 0x7f;
  1241. raw_event.event_id = base_id;
  1242. switch (current_cpu_type()) {
  1243. case CPU_24K:
  1244. if (IS_BOTH_COUNTERS_24K_EVENT(base_id))
  1245. raw_event.cntr_mask = CNTR_EVEN | CNTR_ODD;
  1246. else
  1247. raw_event.cntr_mask =
  1248. raw_id > 127 ? CNTR_ODD : CNTR_EVEN;
  1249. #ifdef CONFIG_MIPS_MT_SMP
  1250. /*
  1251. * This is actually doing nothing. Non-multithreading
  1252. * CPUs will not check and calculate the range.
  1253. */
  1254. raw_event.range = P;
  1255. #endif
  1256. break;
  1257. case CPU_34K:
  1258. if (IS_BOTH_COUNTERS_34K_EVENT(base_id))
  1259. raw_event.cntr_mask = CNTR_EVEN | CNTR_ODD;
  1260. else
  1261. raw_event.cntr_mask =
  1262. raw_id > 127 ? CNTR_ODD : CNTR_EVEN;
  1263. #ifdef CONFIG_MIPS_MT_SMP
  1264. if (IS_RANGE_P_34K_EVENT(raw_id, base_id))
  1265. raw_event.range = P;
  1266. else if (unlikely(IS_RANGE_V_34K_EVENT(raw_id)))
  1267. raw_event.range = V;
  1268. else
  1269. raw_event.range = T;
  1270. #endif
  1271. break;
  1272. case CPU_74K:
  1273. if (IS_BOTH_COUNTERS_74K_EVENT(base_id))
  1274. raw_event.cntr_mask = CNTR_EVEN | CNTR_ODD;
  1275. else
  1276. raw_event.cntr_mask =
  1277. raw_id > 127 ? CNTR_ODD : CNTR_EVEN;
  1278. #ifdef CONFIG_MIPS_MT_SMP
  1279. raw_event.range = P;
  1280. #endif
  1281. break;
  1282. case CPU_1004K:
  1283. if (IS_BOTH_COUNTERS_1004K_EVENT(base_id))
  1284. raw_event.cntr_mask = CNTR_EVEN | CNTR_ODD;
  1285. else
  1286. raw_event.cntr_mask =
  1287. raw_id > 127 ? CNTR_ODD : CNTR_EVEN;
  1288. #ifdef CONFIG_MIPS_MT_SMP
  1289. if (IS_RANGE_P_1004K_EVENT(raw_id, base_id))
  1290. raw_event.range = P;
  1291. else if (unlikely(IS_RANGE_V_1004K_EVENT(raw_id)))
  1292. raw_event.range = V;
  1293. else
  1294. raw_event.range = T;
  1295. #endif
  1296. break;
  1297. }
  1298. return &raw_event;
  1299. }
  1300. static const struct mips_perf_event *octeon_pmu_map_raw_event(u64 config)
  1301. {
  1302. unsigned int raw_id = config & 0xff;
  1303. unsigned int base_id = raw_id & 0x7f;
  1304. raw_event.cntr_mask = CNTR_ALL;
  1305. raw_event.event_id = base_id;
  1306. if (current_cpu_type() == CPU_CAVIUM_OCTEON2) {
  1307. if (base_id > 0x42)
  1308. return ERR_PTR(-EOPNOTSUPP);
  1309. } else {
  1310. if (base_id > 0x3a)
  1311. return ERR_PTR(-EOPNOTSUPP);
  1312. }
  1313. switch (base_id) {
  1314. case 0x00:
  1315. case 0x0f:
  1316. case 0x1e:
  1317. case 0x1f:
  1318. case 0x2f:
  1319. case 0x34:
  1320. case 0x3b ... 0x3f:
  1321. return ERR_PTR(-EOPNOTSUPP);
  1322. default:
  1323. break;
  1324. }
  1325. return &raw_event;
  1326. }
  1327. static int __init
  1328. init_hw_perf_events(void)
  1329. {
  1330. int counters, irq;
  1331. int counter_bits;
  1332. pr_info("Performance counters: ");
  1333. counters = n_counters();
  1334. if (counters == 0) {
  1335. pr_cont("No available PMU.\n");
  1336. return -ENODEV;
  1337. }
  1338. #ifdef CONFIG_MIPS_MT_SMP
  1339. cpu_has_mipsmt_pertccounters = read_c0_config7() & (1<<19);
  1340. if (!cpu_has_mipsmt_pertccounters)
  1341. counters = counters_total_to_per_cpu(counters);
  1342. #endif
  1343. #ifdef MSC01E_INT_BASE
  1344. if (cpu_has_veic) {
  1345. /*
  1346. * Using platform specific interrupt controller defines.
  1347. */
  1348. irq = MSC01E_INT_BASE + MSC01E_INT_PERFCTR;
  1349. } else {
  1350. #endif
  1351. if (cp0_perfcount_irq >= 0)
  1352. irq = MIPS_CPU_IRQ_BASE + cp0_perfcount_irq;
  1353. else
  1354. irq = -1;
  1355. #ifdef MSC01E_INT_BASE
  1356. }
  1357. #endif
  1358. mipspmu.map_raw_event = mipsxx_pmu_map_raw_event;
  1359. switch (current_cpu_type()) {
  1360. case CPU_24K:
  1361. mipspmu.name = "mips/24K";
  1362. mipspmu.general_event_map = &mipsxxcore_event_map;
  1363. mipspmu.cache_event_map = &mipsxxcore_cache_map;
  1364. break;
  1365. case CPU_34K:
  1366. mipspmu.name = "mips/34K";
  1367. mipspmu.general_event_map = &mipsxxcore_event_map;
  1368. mipspmu.cache_event_map = &mipsxxcore_cache_map;
  1369. break;
  1370. case CPU_74K:
  1371. mipspmu.name = "mips/74K";
  1372. mipspmu.general_event_map = &mipsxx74Kcore_event_map;
  1373. mipspmu.cache_event_map = &mipsxx74Kcore_cache_map;
  1374. break;
  1375. case CPU_1004K:
  1376. mipspmu.name = "mips/1004K";
  1377. mipspmu.general_event_map = &mipsxxcore_event_map;
  1378. mipspmu.cache_event_map = &mipsxxcore_cache_map;
  1379. break;
  1380. case CPU_CAVIUM_OCTEON:
  1381. case CPU_CAVIUM_OCTEON_PLUS:
  1382. case CPU_CAVIUM_OCTEON2:
  1383. mipspmu.name = "octeon";
  1384. mipspmu.general_event_map = &octeon_event_map;
  1385. mipspmu.cache_event_map = &octeon_cache_map;
  1386. mipspmu.map_raw_event = octeon_pmu_map_raw_event;
  1387. break;
  1388. default:
  1389. pr_cont("Either hardware does not support performance "
  1390. "counters, or not yet implemented.\n");
  1391. return -ENODEV;
  1392. }
  1393. mipspmu.num_counters = counters;
  1394. mipspmu.irq = irq;
  1395. if (read_c0_perfctrl0() & M_PERFCTL_WIDE) {
  1396. mipspmu.max_period = (1ULL << 63) - 1;
  1397. mipspmu.valid_count = (1ULL << 63) - 1;
  1398. mipspmu.overflow = 1ULL << 63;
  1399. mipspmu.read_counter = mipsxx_pmu_read_counter_64;
  1400. mipspmu.write_counter = mipsxx_pmu_write_counter_64;
  1401. counter_bits = 64;
  1402. } else {
  1403. mipspmu.max_period = (1ULL << 31) - 1;
  1404. mipspmu.valid_count = (1ULL << 31) - 1;
  1405. mipspmu.overflow = 1ULL << 31;
  1406. mipspmu.read_counter = mipsxx_pmu_read_counter;
  1407. mipspmu.write_counter = mipsxx_pmu_write_counter;
  1408. counter_bits = 32;
  1409. }
  1410. on_each_cpu(reset_counters, (void *)(long)counters, 1);
  1411. pr_cont("%s PMU enabled, %d %d-bit counters available to each "
  1412. "CPU, irq %d%s\n", mipspmu.name, counters, counter_bits, irq,
  1413. irq < 0 ? " (share with timer interrupt)" : "");
  1414. perf_pmu_register(&pmu, "cpu", PERF_TYPE_RAW);
  1415. return 0;
  1416. }
  1417. early_initcall(init_hw_perf_events);