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