perf_event_mipsxx.c 47 KB

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