perf_event_mipsxx.c 43 KB

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