perf_event_mipsxx.c 40 KB

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