perf_event_mipsxx.c 44 KB

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