perf_event_mipsxx.c 47 KB

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