perf_event_mipsxx.c 38 KB

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