perf_event_mipsxx.c 47 KB

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