perf_event_mipsxx.c 36 KB

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