perf_event_mipsxx.c 47 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851
  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 ((unsigned int)event->cpu >= nr_cpumask_bits ||
  525. (event->cpu >= 0 && !cpu_online(event->cpu)))
  526. return -ENODEV;
  527. if (!atomic_inc_not_zero(&active_events)) {
  528. mutex_lock(&pmu_reserve_mutex);
  529. if (atomic_read(&active_events) == 0)
  530. err = mipspmu_get_irq();
  531. if (!err)
  532. atomic_inc(&active_events);
  533. mutex_unlock(&pmu_reserve_mutex);
  534. }
  535. if (err)
  536. return err;
  537. return __hw_perf_event_init(event);
  538. }
  539. static struct pmu pmu = {
  540. .pmu_enable = mipspmu_enable,
  541. .pmu_disable = mipspmu_disable,
  542. .event_init = mipspmu_event_init,
  543. .add = mipspmu_add,
  544. .del = mipspmu_del,
  545. .start = mipspmu_start,
  546. .stop = mipspmu_stop,
  547. .read = mipspmu_read,
  548. };
  549. static unsigned int mipspmu_perf_event_encode(const struct mips_perf_event *pev)
  550. {
  551. /*
  552. * Top 8 bits for range, next 16 bits for cntr_mask, lowest 8 bits for
  553. * event_id.
  554. */
  555. #ifdef CONFIG_MIPS_MT_SMP
  556. return ((unsigned int)pev->range << 24) |
  557. (pev->cntr_mask & 0xffff00) |
  558. (pev->event_id & 0xff);
  559. #else
  560. return (pev->cntr_mask & 0xffff00) |
  561. (pev->event_id & 0xff);
  562. #endif
  563. }
  564. static const struct mips_perf_event *mipspmu_map_general_event(int idx)
  565. {
  566. if ((*mipspmu.general_event_map)[idx].cntr_mask == 0)
  567. return ERR_PTR(-EOPNOTSUPP);
  568. return &(*mipspmu.general_event_map)[idx];
  569. }
  570. static const struct mips_perf_event *mipspmu_map_cache_event(u64 config)
  571. {
  572. unsigned int cache_type, cache_op, cache_result;
  573. const struct mips_perf_event *pev;
  574. cache_type = (config >> 0) & 0xff;
  575. if (cache_type >= PERF_COUNT_HW_CACHE_MAX)
  576. return ERR_PTR(-EINVAL);
  577. cache_op = (config >> 8) & 0xff;
  578. if (cache_op >= PERF_COUNT_HW_CACHE_OP_MAX)
  579. return ERR_PTR(-EINVAL);
  580. cache_result = (config >> 16) & 0xff;
  581. if (cache_result >= PERF_COUNT_HW_CACHE_RESULT_MAX)
  582. return ERR_PTR(-EINVAL);
  583. pev = &((*mipspmu.cache_event_map)
  584. [cache_type]
  585. [cache_op]
  586. [cache_result]);
  587. if (pev->cntr_mask == 0)
  588. return ERR_PTR(-EOPNOTSUPP);
  589. return pev;
  590. }
  591. static int validate_group(struct perf_event *event)
  592. {
  593. struct perf_event *sibling, *leader = event->group_leader;
  594. struct cpu_hw_events fake_cpuc;
  595. memset(&fake_cpuc, 0, sizeof(fake_cpuc));
  596. if (mipsxx_pmu_alloc_counter(&fake_cpuc, &leader->hw) < 0)
  597. return -EINVAL;
  598. list_for_each_entry(sibling, &leader->sibling_list, group_entry) {
  599. if (mipsxx_pmu_alloc_counter(&fake_cpuc, &sibling->hw) < 0)
  600. return -EINVAL;
  601. }
  602. if (mipsxx_pmu_alloc_counter(&fake_cpuc, &event->hw) < 0)
  603. return -EINVAL;
  604. return 0;
  605. }
  606. /* This is needed by specific irq handlers in perf_event_*.c */
  607. static void handle_associated_event(struct cpu_hw_events *cpuc,
  608. int idx, struct perf_sample_data *data,
  609. struct pt_regs *regs)
  610. {
  611. struct perf_event *event = cpuc->events[idx];
  612. struct hw_perf_event *hwc = &event->hw;
  613. mipspmu_event_update(event, hwc, idx);
  614. data->period = event->hw.last_period;
  615. if (!mipspmu_event_set_period(event, hwc, idx))
  616. return;
  617. if (perf_event_overflow(event, data, regs))
  618. mipsxx_pmu_disable_event(idx);
  619. }
  620. static int __n_counters(void)
  621. {
  622. if (!cpu_has_perf)
  623. return 0;
  624. if (!(read_c0_perfctrl0() & MIPS_PERFCTRL_M))
  625. return 1;
  626. if (!(read_c0_perfctrl1() & MIPS_PERFCTRL_M))
  627. return 2;
  628. if (!(read_c0_perfctrl2() & MIPS_PERFCTRL_M))
  629. return 3;
  630. return 4;
  631. }
  632. static int n_counters(void)
  633. {
  634. int counters;
  635. switch (current_cpu_type()) {
  636. case CPU_R10000:
  637. counters = 2;
  638. break;
  639. case CPU_R12000:
  640. case CPU_R14000:
  641. case CPU_R16000:
  642. counters = 4;
  643. break;
  644. default:
  645. counters = __n_counters();
  646. }
  647. return counters;
  648. }
  649. static void reset_counters(void *arg)
  650. {
  651. int counters = (int)(long)arg;
  652. switch (counters) {
  653. case 4:
  654. mipsxx_pmu_write_control(3, 0);
  655. mipspmu.write_counter(3, 0);
  656. case 3:
  657. mipsxx_pmu_write_control(2, 0);
  658. mipspmu.write_counter(2, 0);
  659. case 2:
  660. mipsxx_pmu_write_control(1, 0);
  661. mipspmu.write_counter(1, 0);
  662. case 1:
  663. mipsxx_pmu_write_control(0, 0);
  664. mipspmu.write_counter(0, 0);
  665. }
  666. }
  667. /* 24K/34K/1004K/interAptiv/loongson1 cores share the same event map. */
  668. static const struct mips_perf_event mipsxxcore_event_map
  669. [PERF_COUNT_HW_MAX] = {
  670. [PERF_COUNT_HW_CPU_CYCLES] = { 0x00, CNTR_EVEN | CNTR_ODD, P },
  671. [PERF_COUNT_HW_INSTRUCTIONS] = { 0x01, CNTR_EVEN | CNTR_ODD, T },
  672. [PERF_COUNT_HW_BRANCH_INSTRUCTIONS] = { 0x02, CNTR_EVEN, T },
  673. [PERF_COUNT_HW_BRANCH_MISSES] = { 0x02, CNTR_ODD, T },
  674. };
  675. /* 74K/proAptiv core has different branch event code. */
  676. static const struct mips_perf_event mipsxxcore_event_map2
  677. [PERF_COUNT_HW_MAX] = {
  678. [PERF_COUNT_HW_CPU_CYCLES] = { 0x00, CNTR_EVEN | CNTR_ODD, P },
  679. [PERF_COUNT_HW_INSTRUCTIONS] = { 0x01, CNTR_EVEN | CNTR_ODD, T },
  680. [PERF_COUNT_HW_BRANCH_INSTRUCTIONS] = { 0x27, CNTR_EVEN, T },
  681. [PERF_COUNT_HW_BRANCH_MISSES] = { 0x27, CNTR_ODD, T },
  682. };
  683. static const struct mips_perf_event i6400_event_map[PERF_COUNT_HW_MAX] = {
  684. [PERF_COUNT_HW_CPU_CYCLES] = { 0x00, CNTR_EVEN | CNTR_ODD },
  685. [PERF_COUNT_HW_INSTRUCTIONS] = { 0x01, CNTR_EVEN | CNTR_ODD },
  686. /* These only count dcache, not icache */
  687. [PERF_COUNT_HW_CACHE_REFERENCES] = { 0x45, CNTR_EVEN | CNTR_ODD },
  688. [PERF_COUNT_HW_CACHE_MISSES] = { 0x48, CNTR_EVEN | CNTR_ODD },
  689. [PERF_COUNT_HW_BRANCH_INSTRUCTIONS] = { 0x15, CNTR_EVEN | CNTR_ODD },
  690. [PERF_COUNT_HW_BRANCH_MISSES] = { 0x16, CNTR_EVEN | CNTR_ODD },
  691. };
  692. static const struct mips_perf_event loongson3_event_map[PERF_COUNT_HW_MAX] = {
  693. [PERF_COUNT_HW_CPU_CYCLES] = { 0x00, CNTR_EVEN },
  694. [PERF_COUNT_HW_INSTRUCTIONS] = { 0x00, CNTR_ODD },
  695. [PERF_COUNT_HW_BRANCH_INSTRUCTIONS] = { 0x01, CNTR_EVEN },
  696. [PERF_COUNT_HW_BRANCH_MISSES] = { 0x01, CNTR_ODD },
  697. };
  698. static const struct mips_perf_event octeon_event_map[PERF_COUNT_HW_MAX] = {
  699. [PERF_COUNT_HW_CPU_CYCLES] = { 0x01, CNTR_ALL },
  700. [PERF_COUNT_HW_INSTRUCTIONS] = { 0x03, CNTR_ALL },
  701. [PERF_COUNT_HW_CACHE_REFERENCES] = { 0x2b, CNTR_ALL },
  702. [PERF_COUNT_HW_CACHE_MISSES] = { 0x2e, CNTR_ALL },
  703. [PERF_COUNT_HW_BRANCH_INSTRUCTIONS] = { 0x08, CNTR_ALL },
  704. [PERF_COUNT_HW_BRANCH_MISSES] = { 0x09, CNTR_ALL },
  705. [PERF_COUNT_HW_BUS_CYCLES] = { 0x25, CNTR_ALL },
  706. };
  707. static const struct mips_perf_event bmips5000_event_map
  708. [PERF_COUNT_HW_MAX] = {
  709. [PERF_COUNT_HW_CPU_CYCLES] = { 0x00, CNTR_EVEN | CNTR_ODD, T },
  710. [PERF_COUNT_HW_INSTRUCTIONS] = { 0x01, CNTR_EVEN | CNTR_ODD, T },
  711. [PERF_COUNT_HW_BRANCH_MISSES] = { 0x02, CNTR_ODD, T },
  712. };
  713. static const struct mips_perf_event xlp_event_map[PERF_COUNT_HW_MAX] = {
  714. [PERF_COUNT_HW_CPU_CYCLES] = { 0x01, CNTR_ALL },
  715. [PERF_COUNT_HW_INSTRUCTIONS] = { 0x18, CNTR_ALL }, /* PAPI_TOT_INS */
  716. [PERF_COUNT_HW_CACHE_REFERENCES] = { 0x04, CNTR_ALL }, /* PAPI_L1_ICA */
  717. [PERF_COUNT_HW_CACHE_MISSES] = { 0x07, CNTR_ALL }, /* PAPI_L1_ICM */
  718. [PERF_COUNT_HW_BRANCH_INSTRUCTIONS] = { 0x1b, CNTR_ALL }, /* PAPI_BR_CN */
  719. [PERF_COUNT_HW_BRANCH_MISSES] = { 0x1c, CNTR_ALL }, /* PAPI_BR_MSP */
  720. };
  721. /* 24K/34K/1004K/interAptiv/loongson1 cores share the same cache event map. */
  722. static const struct mips_perf_event mipsxxcore_cache_map
  723. [PERF_COUNT_HW_CACHE_MAX]
  724. [PERF_COUNT_HW_CACHE_OP_MAX]
  725. [PERF_COUNT_HW_CACHE_RESULT_MAX] = {
  726. [C(L1D)] = {
  727. /*
  728. * Like some other architectures (e.g. ARM), the performance
  729. * counters don't differentiate between read and write
  730. * accesses/misses, so this isn't strictly correct, but it's the
  731. * best we can do. Writes and reads get combined.
  732. */
  733. [C(OP_READ)] = {
  734. [C(RESULT_ACCESS)] = { 0x0a, CNTR_EVEN, T },
  735. [C(RESULT_MISS)] = { 0x0b, CNTR_EVEN | CNTR_ODD, T },
  736. },
  737. [C(OP_WRITE)] = {
  738. [C(RESULT_ACCESS)] = { 0x0a, CNTR_EVEN, T },
  739. [C(RESULT_MISS)] = { 0x0b, CNTR_EVEN | CNTR_ODD, T },
  740. },
  741. },
  742. [C(L1I)] = {
  743. [C(OP_READ)] = {
  744. [C(RESULT_ACCESS)] = { 0x09, CNTR_EVEN, T },
  745. [C(RESULT_MISS)] = { 0x09, CNTR_ODD, T },
  746. },
  747. [C(OP_WRITE)] = {
  748. [C(RESULT_ACCESS)] = { 0x09, CNTR_EVEN, T },
  749. [C(RESULT_MISS)] = { 0x09, CNTR_ODD, T },
  750. },
  751. [C(OP_PREFETCH)] = {
  752. [C(RESULT_ACCESS)] = { 0x14, CNTR_EVEN, T },
  753. /*
  754. * Note that MIPS has only "hit" events countable for
  755. * the prefetch operation.
  756. */
  757. },
  758. },
  759. [C(LL)] = {
  760. [C(OP_READ)] = {
  761. [C(RESULT_ACCESS)] = { 0x15, CNTR_ODD, P },
  762. [C(RESULT_MISS)] = { 0x16, CNTR_EVEN, P },
  763. },
  764. [C(OP_WRITE)] = {
  765. [C(RESULT_ACCESS)] = { 0x15, CNTR_ODD, P },
  766. [C(RESULT_MISS)] = { 0x16, CNTR_EVEN, P },
  767. },
  768. },
  769. [C(DTLB)] = {
  770. [C(OP_READ)] = {
  771. [C(RESULT_ACCESS)] = { 0x06, CNTR_EVEN, T },
  772. [C(RESULT_MISS)] = { 0x06, CNTR_ODD, T },
  773. },
  774. [C(OP_WRITE)] = {
  775. [C(RESULT_ACCESS)] = { 0x06, CNTR_EVEN, T },
  776. [C(RESULT_MISS)] = { 0x06, CNTR_ODD, T },
  777. },
  778. },
  779. [C(ITLB)] = {
  780. [C(OP_READ)] = {
  781. [C(RESULT_ACCESS)] = { 0x05, CNTR_EVEN, T },
  782. [C(RESULT_MISS)] = { 0x05, CNTR_ODD, T },
  783. },
  784. [C(OP_WRITE)] = {
  785. [C(RESULT_ACCESS)] = { 0x05, CNTR_EVEN, T },
  786. [C(RESULT_MISS)] = { 0x05, CNTR_ODD, T },
  787. },
  788. },
  789. [C(BPU)] = {
  790. /* Using the same code for *HW_BRANCH* */
  791. [C(OP_READ)] = {
  792. [C(RESULT_ACCESS)] = { 0x02, CNTR_EVEN, T },
  793. [C(RESULT_MISS)] = { 0x02, CNTR_ODD, T },
  794. },
  795. [C(OP_WRITE)] = {
  796. [C(RESULT_ACCESS)] = { 0x02, CNTR_EVEN, T },
  797. [C(RESULT_MISS)] = { 0x02, CNTR_ODD, T },
  798. },
  799. },
  800. };
  801. /* 74K/proAptiv core has completely different cache event map. */
  802. static const struct mips_perf_event mipsxxcore_cache_map2
  803. [PERF_COUNT_HW_CACHE_MAX]
  804. [PERF_COUNT_HW_CACHE_OP_MAX]
  805. [PERF_COUNT_HW_CACHE_RESULT_MAX] = {
  806. [C(L1D)] = {
  807. /*
  808. * Like some other architectures (e.g. ARM), the performance
  809. * counters don't differentiate between read and write
  810. * accesses/misses, so this isn't strictly correct, but it's the
  811. * best we can do. Writes and reads get combined.
  812. */
  813. [C(OP_READ)] = {
  814. [C(RESULT_ACCESS)] = { 0x17, CNTR_ODD, T },
  815. [C(RESULT_MISS)] = { 0x18, CNTR_ODD, T },
  816. },
  817. [C(OP_WRITE)] = {
  818. [C(RESULT_ACCESS)] = { 0x17, CNTR_ODD, T },
  819. [C(RESULT_MISS)] = { 0x18, CNTR_ODD, T },
  820. },
  821. },
  822. [C(L1I)] = {
  823. [C(OP_READ)] = {
  824. [C(RESULT_ACCESS)] = { 0x06, CNTR_EVEN, T },
  825. [C(RESULT_MISS)] = { 0x06, CNTR_ODD, T },
  826. },
  827. [C(OP_WRITE)] = {
  828. [C(RESULT_ACCESS)] = { 0x06, CNTR_EVEN, T },
  829. [C(RESULT_MISS)] = { 0x06, CNTR_ODD, T },
  830. },
  831. [C(OP_PREFETCH)] = {
  832. [C(RESULT_ACCESS)] = { 0x34, CNTR_EVEN, T },
  833. /*
  834. * Note that MIPS has only "hit" events countable for
  835. * the prefetch operation.
  836. */
  837. },
  838. },
  839. [C(LL)] = {
  840. [C(OP_READ)] = {
  841. [C(RESULT_ACCESS)] = { 0x1c, CNTR_ODD, P },
  842. [C(RESULT_MISS)] = { 0x1d, CNTR_EVEN, P },
  843. },
  844. [C(OP_WRITE)] = {
  845. [C(RESULT_ACCESS)] = { 0x1c, CNTR_ODD, P },
  846. [C(RESULT_MISS)] = { 0x1d, CNTR_EVEN, P },
  847. },
  848. },
  849. /*
  850. * 74K core does not have specific DTLB events. proAptiv core has
  851. * "speculative" DTLB events which are numbered 0x63 (even/odd) and
  852. * not included here. One can use raw events if really needed.
  853. */
  854. [C(ITLB)] = {
  855. [C(OP_READ)] = {
  856. [C(RESULT_ACCESS)] = { 0x04, CNTR_EVEN, T },
  857. [C(RESULT_MISS)] = { 0x04, CNTR_ODD, T },
  858. },
  859. [C(OP_WRITE)] = {
  860. [C(RESULT_ACCESS)] = { 0x04, CNTR_EVEN, T },
  861. [C(RESULT_MISS)] = { 0x04, CNTR_ODD, T },
  862. },
  863. },
  864. [C(BPU)] = {
  865. /* Using the same code for *HW_BRANCH* */
  866. [C(OP_READ)] = {
  867. [C(RESULT_ACCESS)] = { 0x27, CNTR_EVEN, T },
  868. [C(RESULT_MISS)] = { 0x27, CNTR_ODD, T },
  869. },
  870. [C(OP_WRITE)] = {
  871. [C(RESULT_ACCESS)] = { 0x27, CNTR_EVEN, T },
  872. [C(RESULT_MISS)] = { 0x27, CNTR_ODD, T },
  873. },
  874. },
  875. };
  876. static const struct mips_perf_event i6400_cache_map
  877. [PERF_COUNT_HW_CACHE_MAX]
  878. [PERF_COUNT_HW_CACHE_OP_MAX]
  879. [PERF_COUNT_HW_CACHE_RESULT_MAX] = {
  880. [C(L1D)] = {
  881. [C(OP_READ)] = {
  882. [C(RESULT_ACCESS)] = { 0x46, CNTR_EVEN | CNTR_ODD },
  883. [C(RESULT_MISS)] = { 0x49, CNTR_EVEN | CNTR_ODD },
  884. },
  885. [C(OP_WRITE)] = {
  886. [C(RESULT_ACCESS)] = { 0x47, CNTR_EVEN | CNTR_ODD },
  887. [C(RESULT_MISS)] = { 0x4a, CNTR_EVEN | CNTR_ODD },
  888. },
  889. },
  890. [C(L1I)] = {
  891. [C(OP_READ)] = {
  892. [C(RESULT_ACCESS)] = { 0x84, CNTR_EVEN | CNTR_ODD },
  893. [C(RESULT_MISS)] = { 0x85, CNTR_EVEN | CNTR_ODD },
  894. },
  895. },
  896. [C(DTLB)] = {
  897. /* Can't distinguish read & write */
  898. [C(OP_READ)] = {
  899. [C(RESULT_ACCESS)] = { 0x40, CNTR_EVEN | CNTR_ODD },
  900. [C(RESULT_MISS)] = { 0x41, CNTR_EVEN | CNTR_ODD },
  901. },
  902. [C(OP_WRITE)] = {
  903. [C(RESULT_ACCESS)] = { 0x40, CNTR_EVEN | CNTR_ODD },
  904. [C(RESULT_MISS)] = { 0x41, CNTR_EVEN | CNTR_ODD },
  905. },
  906. },
  907. [C(BPU)] = {
  908. /* Conditional branches / mispredicted */
  909. [C(OP_READ)] = {
  910. [C(RESULT_ACCESS)] = { 0x15, CNTR_EVEN | CNTR_ODD },
  911. [C(RESULT_MISS)] = { 0x16, CNTR_EVEN | CNTR_ODD },
  912. },
  913. },
  914. };
  915. static const struct mips_perf_event loongson3_cache_map
  916. [PERF_COUNT_HW_CACHE_MAX]
  917. [PERF_COUNT_HW_CACHE_OP_MAX]
  918. [PERF_COUNT_HW_CACHE_RESULT_MAX] = {
  919. [C(L1D)] = {
  920. /*
  921. * Like some other architectures (e.g. ARM), the performance
  922. * counters don't differentiate between read and write
  923. * accesses/misses, so this isn't strictly correct, but it's the
  924. * best we can do. Writes and reads get combined.
  925. */
  926. [C(OP_READ)] = {
  927. [C(RESULT_MISS)] = { 0x04, CNTR_ODD },
  928. },
  929. [C(OP_WRITE)] = {
  930. [C(RESULT_MISS)] = { 0x04, CNTR_ODD },
  931. },
  932. },
  933. [C(L1I)] = {
  934. [C(OP_READ)] = {
  935. [C(RESULT_MISS)] = { 0x04, CNTR_EVEN },
  936. },
  937. [C(OP_WRITE)] = {
  938. [C(RESULT_MISS)] = { 0x04, CNTR_EVEN },
  939. },
  940. },
  941. [C(DTLB)] = {
  942. [C(OP_READ)] = {
  943. [C(RESULT_MISS)] = { 0x09, CNTR_ODD },
  944. },
  945. [C(OP_WRITE)] = {
  946. [C(RESULT_MISS)] = { 0x09, CNTR_ODD },
  947. },
  948. },
  949. [C(ITLB)] = {
  950. [C(OP_READ)] = {
  951. [C(RESULT_MISS)] = { 0x0c, CNTR_ODD },
  952. },
  953. [C(OP_WRITE)] = {
  954. [C(RESULT_MISS)] = { 0x0c, CNTR_ODD },
  955. },
  956. },
  957. [C(BPU)] = {
  958. /* Using the same code for *HW_BRANCH* */
  959. [C(OP_READ)] = {
  960. [C(RESULT_ACCESS)] = { 0x02, CNTR_EVEN },
  961. [C(RESULT_MISS)] = { 0x02, CNTR_ODD },
  962. },
  963. [C(OP_WRITE)] = {
  964. [C(RESULT_ACCESS)] = { 0x02, CNTR_EVEN },
  965. [C(RESULT_MISS)] = { 0x02, CNTR_ODD },
  966. },
  967. },
  968. };
  969. /* BMIPS5000 */
  970. static const struct mips_perf_event bmips5000_cache_map
  971. [PERF_COUNT_HW_CACHE_MAX]
  972. [PERF_COUNT_HW_CACHE_OP_MAX]
  973. [PERF_COUNT_HW_CACHE_RESULT_MAX] = {
  974. [C(L1D)] = {
  975. /*
  976. * Like some other architectures (e.g. ARM), the performance
  977. * counters don't differentiate between read and write
  978. * accesses/misses, so this isn't strictly correct, but it's the
  979. * best we can do. Writes and reads get combined.
  980. */
  981. [C(OP_READ)] = {
  982. [C(RESULT_ACCESS)] = { 12, CNTR_EVEN, T },
  983. [C(RESULT_MISS)] = { 12, CNTR_ODD, T },
  984. },
  985. [C(OP_WRITE)] = {
  986. [C(RESULT_ACCESS)] = { 12, CNTR_EVEN, T },
  987. [C(RESULT_MISS)] = { 12, CNTR_ODD, T },
  988. },
  989. },
  990. [C(L1I)] = {
  991. [C(OP_READ)] = {
  992. [C(RESULT_ACCESS)] = { 10, CNTR_EVEN, T },
  993. [C(RESULT_MISS)] = { 10, CNTR_ODD, T },
  994. },
  995. [C(OP_WRITE)] = {
  996. [C(RESULT_ACCESS)] = { 10, CNTR_EVEN, T },
  997. [C(RESULT_MISS)] = { 10, CNTR_ODD, T },
  998. },
  999. [C(OP_PREFETCH)] = {
  1000. [C(RESULT_ACCESS)] = { 23, CNTR_EVEN, T },
  1001. /*
  1002. * Note that MIPS has only "hit" events countable for
  1003. * the prefetch operation.
  1004. */
  1005. },
  1006. },
  1007. [C(LL)] = {
  1008. [C(OP_READ)] = {
  1009. [C(RESULT_ACCESS)] = { 28, CNTR_EVEN, P },
  1010. [C(RESULT_MISS)] = { 28, CNTR_ODD, P },
  1011. },
  1012. [C(OP_WRITE)] = {
  1013. [C(RESULT_ACCESS)] = { 28, CNTR_EVEN, P },
  1014. [C(RESULT_MISS)] = { 28, CNTR_ODD, P },
  1015. },
  1016. },
  1017. [C(BPU)] = {
  1018. /* Using the same code for *HW_BRANCH* */
  1019. [C(OP_READ)] = {
  1020. [C(RESULT_MISS)] = { 0x02, CNTR_ODD, T },
  1021. },
  1022. [C(OP_WRITE)] = {
  1023. [C(RESULT_MISS)] = { 0x02, CNTR_ODD, T },
  1024. },
  1025. },
  1026. };
  1027. static const struct mips_perf_event octeon_cache_map
  1028. [PERF_COUNT_HW_CACHE_MAX]
  1029. [PERF_COUNT_HW_CACHE_OP_MAX]
  1030. [PERF_COUNT_HW_CACHE_RESULT_MAX] = {
  1031. [C(L1D)] = {
  1032. [C(OP_READ)] = {
  1033. [C(RESULT_ACCESS)] = { 0x2b, CNTR_ALL },
  1034. [C(RESULT_MISS)] = { 0x2e, CNTR_ALL },
  1035. },
  1036. [C(OP_WRITE)] = {
  1037. [C(RESULT_ACCESS)] = { 0x30, CNTR_ALL },
  1038. },
  1039. },
  1040. [C(L1I)] = {
  1041. [C(OP_READ)] = {
  1042. [C(RESULT_ACCESS)] = { 0x18, CNTR_ALL },
  1043. },
  1044. [C(OP_PREFETCH)] = {
  1045. [C(RESULT_ACCESS)] = { 0x19, CNTR_ALL },
  1046. },
  1047. },
  1048. [C(DTLB)] = {
  1049. /*
  1050. * Only general DTLB misses are counted use the same event for
  1051. * read and write.
  1052. */
  1053. [C(OP_READ)] = {
  1054. [C(RESULT_MISS)] = { 0x35, CNTR_ALL },
  1055. },
  1056. [C(OP_WRITE)] = {
  1057. [C(RESULT_MISS)] = { 0x35, CNTR_ALL },
  1058. },
  1059. },
  1060. [C(ITLB)] = {
  1061. [C(OP_READ)] = {
  1062. [C(RESULT_MISS)] = { 0x37, CNTR_ALL },
  1063. },
  1064. },
  1065. };
  1066. static const struct mips_perf_event xlp_cache_map
  1067. [PERF_COUNT_HW_CACHE_MAX]
  1068. [PERF_COUNT_HW_CACHE_OP_MAX]
  1069. [PERF_COUNT_HW_CACHE_RESULT_MAX] = {
  1070. [C(L1D)] = {
  1071. [C(OP_READ)] = {
  1072. [C(RESULT_ACCESS)] = { 0x31, CNTR_ALL }, /* PAPI_L1_DCR */
  1073. [C(RESULT_MISS)] = { 0x30, CNTR_ALL }, /* PAPI_L1_LDM */
  1074. },
  1075. [C(OP_WRITE)] = {
  1076. [C(RESULT_ACCESS)] = { 0x2f, CNTR_ALL }, /* PAPI_L1_DCW */
  1077. [C(RESULT_MISS)] = { 0x2e, CNTR_ALL }, /* PAPI_L1_STM */
  1078. },
  1079. },
  1080. [C(L1I)] = {
  1081. [C(OP_READ)] = {
  1082. [C(RESULT_ACCESS)] = { 0x04, CNTR_ALL }, /* PAPI_L1_ICA */
  1083. [C(RESULT_MISS)] = { 0x07, CNTR_ALL }, /* PAPI_L1_ICM */
  1084. },
  1085. },
  1086. [C(LL)] = {
  1087. [C(OP_READ)] = {
  1088. [C(RESULT_ACCESS)] = { 0x35, CNTR_ALL }, /* PAPI_L2_DCR */
  1089. [C(RESULT_MISS)] = { 0x37, CNTR_ALL }, /* PAPI_L2_LDM */
  1090. },
  1091. [C(OP_WRITE)] = {
  1092. [C(RESULT_ACCESS)] = { 0x34, CNTR_ALL }, /* PAPI_L2_DCA */
  1093. [C(RESULT_MISS)] = { 0x36, CNTR_ALL }, /* PAPI_L2_DCM */
  1094. },
  1095. },
  1096. [C(DTLB)] = {
  1097. /*
  1098. * Only general DTLB misses are counted use the same event for
  1099. * read and write.
  1100. */
  1101. [C(OP_READ)] = {
  1102. [C(RESULT_MISS)] = { 0x2d, CNTR_ALL }, /* PAPI_TLB_DM */
  1103. },
  1104. [C(OP_WRITE)] = {
  1105. [C(RESULT_MISS)] = { 0x2d, CNTR_ALL }, /* PAPI_TLB_DM */
  1106. },
  1107. },
  1108. [C(ITLB)] = {
  1109. [C(OP_READ)] = {
  1110. [C(RESULT_MISS)] = { 0x08, CNTR_ALL }, /* PAPI_TLB_IM */
  1111. },
  1112. [C(OP_WRITE)] = {
  1113. [C(RESULT_MISS)] = { 0x08, CNTR_ALL }, /* PAPI_TLB_IM */
  1114. },
  1115. },
  1116. [C(BPU)] = {
  1117. [C(OP_READ)] = {
  1118. [C(RESULT_MISS)] = { 0x25, CNTR_ALL },
  1119. },
  1120. },
  1121. };
  1122. #ifdef CONFIG_MIPS_MT_SMP
  1123. static void check_and_calc_range(struct perf_event *event,
  1124. const struct mips_perf_event *pev)
  1125. {
  1126. struct hw_perf_event *hwc = &event->hw;
  1127. if (event->cpu >= 0) {
  1128. if (pev->range > V) {
  1129. /*
  1130. * The user selected an event that is processor
  1131. * wide, while expecting it to be VPE wide.
  1132. */
  1133. hwc->config_base |= M_TC_EN_ALL;
  1134. } else {
  1135. /*
  1136. * FIXME: cpu_data[event->cpu].vpe_id reports 0
  1137. * for both CPUs.
  1138. */
  1139. hwc->config_base |= M_PERFCTL_VPEID(event->cpu);
  1140. hwc->config_base |= M_TC_EN_VPE;
  1141. }
  1142. } else
  1143. hwc->config_base |= M_TC_EN_ALL;
  1144. }
  1145. #else
  1146. static void check_and_calc_range(struct perf_event *event,
  1147. const struct mips_perf_event *pev)
  1148. {
  1149. }
  1150. #endif
  1151. static int __hw_perf_event_init(struct perf_event *event)
  1152. {
  1153. struct perf_event_attr *attr = &event->attr;
  1154. struct hw_perf_event *hwc = &event->hw;
  1155. const struct mips_perf_event *pev;
  1156. int err;
  1157. /* Returning MIPS event descriptor for generic perf event. */
  1158. if (PERF_TYPE_HARDWARE == event->attr.type) {
  1159. if (event->attr.config >= PERF_COUNT_HW_MAX)
  1160. return -EINVAL;
  1161. pev = mipspmu_map_general_event(event->attr.config);
  1162. } else if (PERF_TYPE_HW_CACHE == event->attr.type) {
  1163. pev = mipspmu_map_cache_event(event->attr.config);
  1164. } else if (PERF_TYPE_RAW == event->attr.type) {
  1165. /* We are working on the global raw event. */
  1166. mutex_lock(&raw_event_mutex);
  1167. pev = mipspmu.map_raw_event(event->attr.config);
  1168. } else {
  1169. /* The event type is not (yet) supported. */
  1170. return -EOPNOTSUPP;
  1171. }
  1172. if (IS_ERR(pev)) {
  1173. if (PERF_TYPE_RAW == event->attr.type)
  1174. mutex_unlock(&raw_event_mutex);
  1175. return PTR_ERR(pev);
  1176. }
  1177. /*
  1178. * We allow max flexibility on how each individual counter shared
  1179. * by the single CPU operates (the mode exclusion and the range).
  1180. */
  1181. hwc->config_base = MIPS_PERFCTRL_IE;
  1182. /* Calculate range bits and validate it. */
  1183. if (num_possible_cpus() > 1)
  1184. check_and_calc_range(event, pev);
  1185. hwc->event_base = mipspmu_perf_event_encode(pev);
  1186. if (PERF_TYPE_RAW == event->attr.type)
  1187. mutex_unlock(&raw_event_mutex);
  1188. if (!attr->exclude_user)
  1189. hwc->config_base |= MIPS_PERFCTRL_U;
  1190. if (!attr->exclude_kernel) {
  1191. hwc->config_base |= MIPS_PERFCTRL_K;
  1192. /* MIPS kernel mode: KSU == 00b || EXL == 1 || ERL == 1 */
  1193. hwc->config_base |= MIPS_PERFCTRL_EXL;
  1194. }
  1195. if (!attr->exclude_hv)
  1196. hwc->config_base |= MIPS_PERFCTRL_S;
  1197. hwc->config_base &= M_PERFCTL_CONFIG_MASK;
  1198. /*
  1199. * The event can belong to another cpu. We do not assign a local
  1200. * counter for it for now.
  1201. */
  1202. hwc->idx = -1;
  1203. hwc->config = 0;
  1204. if (!hwc->sample_period) {
  1205. hwc->sample_period = mipspmu.max_period;
  1206. hwc->last_period = hwc->sample_period;
  1207. local64_set(&hwc->period_left, hwc->sample_period);
  1208. }
  1209. err = 0;
  1210. if (event->group_leader != event)
  1211. err = validate_group(event);
  1212. event->destroy = hw_perf_event_destroy;
  1213. if (err)
  1214. event->destroy(event);
  1215. return err;
  1216. }
  1217. static void pause_local_counters(void)
  1218. {
  1219. struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
  1220. int ctr = mipspmu.num_counters;
  1221. unsigned long flags;
  1222. local_irq_save(flags);
  1223. do {
  1224. ctr--;
  1225. cpuc->saved_ctrl[ctr] = mipsxx_pmu_read_control(ctr);
  1226. mipsxx_pmu_write_control(ctr, cpuc->saved_ctrl[ctr] &
  1227. ~M_PERFCTL_COUNT_EVENT_WHENEVER);
  1228. } while (ctr > 0);
  1229. local_irq_restore(flags);
  1230. }
  1231. static void resume_local_counters(void)
  1232. {
  1233. struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
  1234. int ctr = mipspmu.num_counters;
  1235. do {
  1236. ctr--;
  1237. mipsxx_pmu_write_control(ctr, cpuc->saved_ctrl[ctr]);
  1238. } while (ctr > 0);
  1239. }
  1240. static int mipsxx_pmu_handle_shared_irq(void)
  1241. {
  1242. struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
  1243. struct perf_sample_data data;
  1244. unsigned int counters = mipspmu.num_counters;
  1245. u64 counter;
  1246. int handled = IRQ_NONE;
  1247. struct pt_regs *regs;
  1248. if (cpu_has_perf_cntr_intr_bit && !(read_c0_cause() & CAUSEF_PCI))
  1249. return handled;
  1250. /*
  1251. * First we pause the local counters, so that when we are locked
  1252. * here, the counters are all paused. When it gets locked due to
  1253. * perf_disable(), the timer interrupt handler will be delayed.
  1254. *
  1255. * See also mipsxx_pmu_start().
  1256. */
  1257. pause_local_counters();
  1258. #ifdef CONFIG_MIPS_PERF_SHARED_TC_COUNTERS
  1259. read_lock(&pmuint_rwlock);
  1260. #endif
  1261. regs = get_irq_regs();
  1262. perf_sample_data_init(&data, 0, 0);
  1263. switch (counters) {
  1264. #define HANDLE_COUNTER(n) \
  1265. case n + 1: \
  1266. if (test_bit(n, cpuc->used_mask)) { \
  1267. counter = mipspmu.read_counter(n); \
  1268. if (counter & mipspmu.overflow) { \
  1269. handle_associated_event(cpuc, n, &data, regs); \
  1270. handled = IRQ_HANDLED; \
  1271. } \
  1272. }
  1273. HANDLE_COUNTER(3)
  1274. HANDLE_COUNTER(2)
  1275. HANDLE_COUNTER(1)
  1276. HANDLE_COUNTER(0)
  1277. }
  1278. #ifdef CONFIG_MIPS_PERF_SHARED_TC_COUNTERS
  1279. read_unlock(&pmuint_rwlock);
  1280. #endif
  1281. resume_local_counters();
  1282. /*
  1283. * Do all the work for the pending perf events. We can do this
  1284. * in here because the performance counter interrupt is a regular
  1285. * interrupt, not NMI.
  1286. */
  1287. if (handled == IRQ_HANDLED)
  1288. irq_work_run();
  1289. return handled;
  1290. }
  1291. static irqreturn_t mipsxx_pmu_handle_irq(int irq, void *dev)
  1292. {
  1293. return mipsxx_pmu_handle_shared_irq();
  1294. }
  1295. /* 24K */
  1296. #define IS_BOTH_COUNTERS_24K_EVENT(b) \
  1297. ((b) == 0 || (b) == 1 || (b) == 11)
  1298. /* 34K */
  1299. #define IS_BOTH_COUNTERS_34K_EVENT(b) \
  1300. ((b) == 0 || (b) == 1 || (b) == 11)
  1301. #ifdef CONFIG_MIPS_MT_SMP
  1302. #define IS_RANGE_P_34K_EVENT(r, b) \
  1303. ((b) == 0 || (r) == 18 || (b) == 21 || (b) == 22 || \
  1304. (b) == 25 || (b) == 39 || (r) == 44 || (r) == 174 || \
  1305. (r) == 176 || ((b) >= 50 && (b) <= 55) || \
  1306. ((b) >= 64 && (b) <= 67))
  1307. #define IS_RANGE_V_34K_EVENT(r) ((r) == 47)
  1308. #endif
  1309. /* 74K */
  1310. #define IS_BOTH_COUNTERS_74K_EVENT(b) \
  1311. ((b) == 0 || (b) == 1)
  1312. /* proAptiv */
  1313. #define IS_BOTH_COUNTERS_PROAPTIV_EVENT(b) \
  1314. ((b) == 0 || (b) == 1)
  1315. /* P5600 */
  1316. #define IS_BOTH_COUNTERS_P5600_EVENT(b) \
  1317. ((b) == 0 || (b) == 1)
  1318. /* 1004K */
  1319. #define IS_BOTH_COUNTERS_1004K_EVENT(b) \
  1320. ((b) == 0 || (b) == 1 || (b) == 11)
  1321. #ifdef CONFIG_MIPS_MT_SMP
  1322. #define IS_RANGE_P_1004K_EVENT(r, b) \
  1323. ((b) == 0 || (r) == 18 || (b) == 21 || (b) == 22 || \
  1324. (b) == 25 || (b) == 36 || (b) == 39 || (r) == 44 || \
  1325. (r) == 174 || (r) == 176 || ((b) >= 50 && (b) <= 59) || \
  1326. (r) == 188 || (b) == 61 || (b) == 62 || \
  1327. ((b) >= 64 && (b) <= 67))
  1328. #define IS_RANGE_V_1004K_EVENT(r) ((r) == 47)
  1329. #endif
  1330. /* interAptiv */
  1331. #define IS_BOTH_COUNTERS_INTERAPTIV_EVENT(b) \
  1332. ((b) == 0 || (b) == 1 || (b) == 11)
  1333. #ifdef CONFIG_MIPS_MT_SMP
  1334. /* The P/V/T info is not provided for "(b) == 38" in SUM, assume P. */
  1335. #define IS_RANGE_P_INTERAPTIV_EVENT(r, b) \
  1336. ((b) == 0 || (r) == 18 || (b) == 21 || (b) == 22 || \
  1337. (b) == 25 || (b) == 36 || (b) == 38 || (b) == 39 || \
  1338. (r) == 44 || (r) == 174 || (r) == 176 || ((b) >= 50 && \
  1339. (b) <= 59) || (r) == 188 || (b) == 61 || (b) == 62 || \
  1340. ((b) >= 64 && (b) <= 67))
  1341. #define IS_RANGE_V_INTERAPTIV_EVENT(r) ((r) == 47 || (r) == 175)
  1342. #endif
  1343. /* BMIPS5000 */
  1344. #define IS_BOTH_COUNTERS_BMIPS5000_EVENT(b) \
  1345. ((b) == 0 || (b) == 1)
  1346. /*
  1347. * For most cores the user can use 0-255 raw events, where 0-127 for the events
  1348. * of even counters, and 128-255 for odd counters. Note that bit 7 is used to
  1349. * indicate the even/odd bank selector. So, for example, when user wants to take
  1350. * the Event Num of 15 for odd counters (by referring to the user manual), then
  1351. * 128 needs to be added to 15 as the input for the event config, i.e., 143 (0x8F)
  1352. * to be used.
  1353. *
  1354. * Some newer cores have even more events, in which case the user can use raw
  1355. * events 0-511, where 0-255 are for the events of even counters, and 256-511
  1356. * are for odd counters, so bit 8 is used to indicate the even/odd bank selector.
  1357. */
  1358. static const struct mips_perf_event *mipsxx_pmu_map_raw_event(u64 config)
  1359. {
  1360. /* currently most cores have 7-bit event numbers */
  1361. unsigned int raw_id = config & 0xff;
  1362. unsigned int base_id = raw_id & 0x7f;
  1363. switch (current_cpu_type()) {
  1364. case CPU_24K:
  1365. if (IS_BOTH_COUNTERS_24K_EVENT(base_id))
  1366. raw_event.cntr_mask = CNTR_EVEN | CNTR_ODD;
  1367. else
  1368. raw_event.cntr_mask =
  1369. raw_id > 127 ? CNTR_ODD : CNTR_EVEN;
  1370. #ifdef CONFIG_MIPS_MT_SMP
  1371. /*
  1372. * This is actually doing nothing. Non-multithreading
  1373. * CPUs will not check and calculate the range.
  1374. */
  1375. raw_event.range = P;
  1376. #endif
  1377. break;
  1378. case CPU_34K:
  1379. if (IS_BOTH_COUNTERS_34K_EVENT(base_id))
  1380. raw_event.cntr_mask = CNTR_EVEN | CNTR_ODD;
  1381. else
  1382. raw_event.cntr_mask =
  1383. raw_id > 127 ? CNTR_ODD : CNTR_EVEN;
  1384. #ifdef CONFIG_MIPS_MT_SMP
  1385. if (IS_RANGE_P_34K_EVENT(raw_id, base_id))
  1386. raw_event.range = P;
  1387. else if (unlikely(IS_RANGE_V_34K_EVENT(raw_id)))
  1388. raw_event.range = V;
  1389. else
  1390. raw_event.range = T;
  1391. #endif
  1392. break;
  1393. case CPU_74K:
  1394. case CPU_1074K:
  1395. if (IS_BOTH_COUNTERS_74K_EVENT(base_id))
  1396. raw_event.cntr_mask = CNTR_EVEN | CNTR_ODD;
  1397. else
  1398. raw_event.cntr_mask =
  1399. raw_id > 127 ? CNTR_ODD : CNTR_EVEN;
  1400. #ifdef CONFIG_MIPS_MT_SMP
  1401. raw_event.range = P;
  1402. #endif
  1403. break;
  1404. case CPU_PROAPTIV:
  1405. if (IS_BOTH_COUNTERS_PROAPTIV_EVENT(base_id))
  1406. raw_event.cntr_mask = CNTR_EVEN | CNTR_ODD;
  1407. else
  1408. raw_event.cntr_mask =
  1409. raw_id > 127 ? CNTR_ODD : CNTR_EVEN;
  1410. #ifdef CONFIG_MIPS_MT_SMP
  1411. raw_event.range = P;
  1412. #endif
  1413. break;
  1414. case CPU_P5600:
  1415. case CPU_P6600:
  1416. case CPU_I6400:
  1417. /* 8-bit event numbers */
  1418. raw_id = config & 0x1ff;
  1419. base_id = raw_id & 0xff;
  1420. if (IS_BOTH_COUNTERS_P5600_EVENT(base_id))
  1421. raw_event.cntr_mask = CNTR_EVEN | CNTR_ODD;
  1422. else
  1423. raw_event.cntr_mask =
  1424. raw_id > 255 ? CNTR_ODD : CNTR_EVEN;
  1425. #ifdef CONFIG_MIPS_MT_SMP
  1426. raw_event.range = P;
  1427. #endif
  1428. break;
  1429. case CPU_1004K:
  1430. if (IS_BOTH_COUNTERS_1004K_EVENT(base_id))
  1431. raw_event.cntr_mask = CNTR_EVEN | CNTR_ODD;
  1432. else
  1433. raw_event.cntr_mask =
  1434. raw_id > 127 ? CNTR_ODD : CNTR_EVEN;
  1435. #ifdef CONFIG_MIPS_MT_SMP
  1436. if (IS_RANGE_P_1004K_EVENT(raw_id, base_id))
  1437. raw_event.range = P;
  1438. else if (unlikely(IS_RANGE_V_1004K_EVENT(raw_id)))
  1439. raw_event.range = V;
  1440. else
  1441. raw_event.range = T;
  1442. #endif
  1443. break;
  1444. case CPU_INTERAPTIV:
  1445. if (IS_BOTH_COUNTERS_INTERAPTIV_EVENT(base_id))
  1446. raw_event.cntr_mask = CNTR_EVEN | CNTR_ODD;
  1447. else
  1448. raw_event.cntr_mask =
  1449. raw_id > 127 ? CNTR_ODD : CNTR_EVEN;
  1450. #ifdef CONFIG_MIPS_MT_SMP
  1451. if (IS_RANGE_P_INTERAPTIV_EVENT(raw_id, base_id))
  1452. raw_event.range = P;
  1453. else if (unlikely(IS_RANGE_V_INTERAPTIV_EVENT(raw_id)))
  1454. raw_event.range = V;
  1455. else
  1456. raw_event.range = T;
  1457. #endif
  1458. break;
  1459. case CPU_BMIPS5000:
  1460. if (IS_BOTH_COUNTERS_BMIPS5000_EVENT(base_id))
  1461. raw_event.cntr_mask = CNTR_EVEN | CNTR_ODD;
  1462. else
  1463. raw_event.cntr_mask =
  1464. raw_id > 127 ? CNTR_ODD : CNTR_EVEN;
  1465. break;
  1466. case CPU_LOONGSON3:
  1467. raw_event.cntr_mask = raw_id > 127 ? CNTR_ODD : CNTR_EVEN;
  1468. break;
  1469. }
  1470. raw_event.event_id = base_id;
  1471. return &raw_event;
  1472. }
  1473. static const struct mips_perf_event *octeon_pmu_map_raw_event(u64 config)
  1474. {
  1475. unsigned int raw_id = config & 0xff;
  1476. unsigned int base_id = raw_id & 0x7f;
  1477. raw_event.cntr_mask = CNTR_ALL;
  1478. raw_event.event_id = base_id;
  1479. if (current_cpu_type() == CPU_CAVIUM_OCTEON2) {
  1480. if (base_id > 0x42)
  1481. return ERR_PTR(-EOPNOTSUPP);
  1482. } else {
  1483. if (base_id > 0x3a)
  1484. return ERR_PTR(-EOPNOTSUPP);
  1485. }
  1486. switch (base_id) {
  1487. case 0x00:
  1488. case 0x0f:
  1489. case 0x1e:
  1490. case 0x1f:
  1491. case 0x2f:
  1492. case 0x34:
  1493. case 0x3b ... 0x3f:
  1494. return ERR_PTR(-EOPNOTSUPP);
  1495. default:
  1496. break;
  1497. }
  1498. return &raw_event;
  1499. }
  1500. static const struct mips_perf_event *xlp_pmu_map_raw_event(u64 config)
  1501. {
  1502. unsigned int raw_id = config & 0xff;
  1503. /* Only 1-63 are defined */
  1504. if ((raw_id < 0x01) || (raw_id > 0x3f))
  1505. return ERR_PTR(-EOPNOTSUPP);
  1506. raw_event.cntr_mask = CNTR_ALL;
  1507. raw_event.event_id = raw_id;
  1508. return &raw_event;
  1509. }
  1510. static int __init
  1511. init_hw_perf_events(void)
  1512. {
  1513. int counters, irq;
  1514. int counter_bits;
  1515. pr_info("Performance counters: ");
  1516. counters = n_counters();
  1517. if (counters == 0) {
  1518. pr_cont("No available PMU.\n");
  1519. return -ENODEV;
  1520. }
  1521. #ifdef CONFIG_MIPS_PERF_SHARED_TC_COUNTERS
  1522. cpu_has_mipsmt_pertccounters = read_c0_config7() & (1<<19);
  1523. if (!cpu_has_mipsmt_pertccounters)
  1524. counters = counters_total_to_per_cpu(counters);
  1525. #endif
  1526. if (get_c0_perfcount_int)
  1527. irq = get_c0_perfcount_int();
  1528. else if (cp0_perfcount_irq >= 0)
  1529. irq = MIPS_CPU_IRQ_BASE + cp0_perfcount_irq;
  1530. else
  1531. irq = -1;
  1532. mipspmu.map_raw_event = mipsxx_pmu_map_raw_event;
  1533. switch (current_cpu_type()) {
  1534. case CPU_24K:
  1535. mipspmu.name = "mips/24K";
  1536. mipspmu.general_event_map = &mipsxxcore_event_map;
  1537. mipspmu.cache_event_map = &mipsxxcore_cache_map;
  1538. break;
  1539. case CPU_34K:
  1540. mipspmu.name = "mips/34K";
  1541. mipspmu.general_event_map = &mipsxxcore_event_map;
  1542. mipspmu.cache_event_map = &mipsxxcore_cache_map;
  1543. break;
  1544. case CPU_74K:
  1545. mipspmu.name = "mips/74K";
  1546. mipspmu.general_event_map = &mipsxxcore_event_map2;
  1547. mipspmu.cache_event_map = &mipsxxcore_cache_map2;
  1548. break;
  1549. case CPU_PROAPTIV:
  1550. mipspmu.name = "mips/proAptiv";
  1551. mipspmu.general_event_map = &mipsxxcore_event_map2;
  1552. mipspmu.cache_event_map = &mipsxxcore_cache_map2;
  1553. break;
  1554. case CPU_P5600:
  1555. mipspmu.name = "mips/P5600";
  1556. mipspmu.general_event_map = &mipsxxcore_event_map2;
  1557. mipspmu.cache_event_map = &mipsxxcore_cache_map2;
  1558. break;
  1559. case CPU_P6600:
  1560. mipspmu.name = "mips/P6600";
  1561. mipspmu.general_event_map = &mipsxxcore_event_map2;
  1562. mipspmu.cache_event_map = &mipsxxcore_cache_map2;
  1563. break;
  1564. case CPU_I6400:
  1565. mipspmu.name = "mips/I6400";
  1566. mipspmu.general_event_map = &i6400_event_map;
  1567. mipspmu.cache_event_map = &i6400_cache_map;
  1568. break;
  1569. case CPU_1004K:
  1570. mipspmu.name = "mips/1004K";
  1571. mipspmu.general_event_map = &mipsxxcore_event_map;
  1572. mipspmu.cache_event_map = &mipsxxcore_cache_map;
  1573. break;
  1574. case CPU_1074K:
  1575. mipspmu.name = "mips/1074K";
  1576. mipspmu.general_event_map = &mipsxxcore_event_map;
  1577. mipspmu.cache_event_map = &mipsxxcore_cache_map;
  1578. break;
  1579. case CPU_INTERAPTIV:
  1580. mipspmu.name = "mips/interAptiv";
  1581. mipspmu.general_event_map = &mipsxxcore_event_map;
  1582. mipspmu.cache_event_map = &mipsxxcore_cache_map;
  1583. break;
  1584. case CPU_LOONGSON1:
  1585. mipspmu.name = "mips/loongson1";
  1586. mipspmu.general_event_map = &mipsxxcore_event_map;
  1587. mipspmu.cache_event_map = &mipsxxcore_cache_map;
  1588. break;
  1589. case CPU_LOONGSON3:
  1590. mipspmu.name = "mips/loongson3";
  1591. mipspmu.general_event_map = &loongson3_event_map;
  1592. mipspmu.cache_event_map = &loongson3_cache_map;
  1593. break;
  1594. case CPU_CAVIUM_OCTEON:
  1595. case CPU_CAVIUM_OCTEON_PLUS:
  1596. case CPU_CAVIUM_OCTEON2:
  1597. mipspmu.name = "octeon";
  1598. mipspmu.general_event_map = &octeon_event_map;
  1599. mipspmu.cache_event_map = &octeon_cache_map;
  1600. mipspmu.map_raw_event = octeon_pmu_map_raw_event;
  1601. break;
  1602. case CPU_BMIPS5000:
  1603. mipspmu.name = "BMIPS5000";
  1604. mipspmu.general_event_map = &bmips5000_event_map;
  1605. mipspmu.cache_event_map = &bmips5000_cache_map;
  1606. break;
  1607. case CPU_XLP:
  1608. mipspmu.name = "xlp";
  1609. mipspmu.general_event_map = &xlp_event_map;
  1610. mipspmu.cache_event_map = &xlp_cache_map;
  1611. mipspmu.map_raw_event = xlp_pmu_map_raw_event;
  1612. break;
  1613. default:
  1614. pr_cont("Either hardware does not support performance "
  1615. "counters, or not yet implemented.\n");
  1616. return -ENODEV;
  1617. }
  1618. mipspmu.num_counters = counters;
  1619. mipspmu.irq = irq;
  1620. if (read_c0_perfctrl0() & MIPS_PERFCTRL_W) {
  1621. mipspmu.max_period = (1ULL << 63) - 1;
  1622. mipspmu.valid_count = (1ULL << 63) - 1;
  1623. mipspmu.overflow = 1ULL << 63;
  1624. mipspmu.read_counter = mipsxx_pmu_read_counter_64;
  1625. mipspmu.write_counter = mipsxx_pmu_write_counter_64;
  1626. counter_bits = 64;
  1627. } else {
  1628. mipspmu.max_period = (1ULL << 31) - 1;
  1629. mipspmu.valid_count = (1ULL << 31) - 1;
  1630. mipspmu.overflow = 1ULL << 31;
  1631. mipspmu.read_counter = mipsxx_pmu_read_counter;
  1632. mipspmu.write_counter = mipsxx_pmu_write_counter;
  1633. counter_bits = 32;
  1634. }
  1635. on_each_cpu(reset_counters, (void *)(long)counters, 1);
  1636. pr_cont("%s PMU enabled, %d %d-bit counters available to each "
  1637. "CPU, irq %d%s\n", mipspmu.name, counters, counter_bits, irq,
  1638. irq < 0 ? " (share with timer interrupt)" : "");
  1639. perf_pmu_register(&pmu, "cpu", PERF_TYPE_RAW);
  1640. return 0;
  1641. }
  1642. early_initcall(init_hw_perf_events);