perf_event_mipsxx.c 53 KB

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