perf_event_mipsxx.c 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620
  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. err = __hw_perf_event_init(event);
  538. if (err)
  539. hw_perf_event_destroy(event);
  540. return err;
  541. }
  542. static struct pmu pmu = {
  543. .pmu_enable = mipspmu_enable,
  544. .pmu_disable = mipspmu_disable,
  545. .event_init = mipspmu_event_init,
  546. .add = mipspmu_add,
  547. .del = mipspmu_del,
  548. .start = mipspmu_start,
  549. .stop = mipspmu_stop,
  550. .read = mipspmu_read,
  551. };
  552. static unsigned int mipspmu_perf_event_encode(const struct mips_perf_event *pev)
  553. {
  554. /*
  555. * Top 8 bits for range, next 16 bits for cntr_mask, lowest 8 bits for
  556. * event_id.
  557. */
  558. #ifdef CONFIG_MIPS_MT_SMP
  559. return ((unsigned int)pev->range << 24) |
  560. (pev->cntr_mask & 0xffff00) |
  561. (pev->event_id & 0xff);
  562. #else
  563. return (pev->cntr_mask & 0xffff00) |
  564. (pev->event_id & 0xff);
  565. #endif
  566. }
  567. static const struct mips_perf_event *mipspmu_map_general_event(int idx)
  568. {
  569. const struct mips_perf_event *pev;
  570. pev = ((*mipspmu.general_event_map)[idx].event_id ==
  571. UNSUPPORTED_PERF_EVENT_ID ? ERR_PTR(-EOPNOTSUPP) :
  572. &(*mipspmu.general_event_map)[idx]);
  573. return pev;
  574. }
  575. static const struct mips_perf_event *mipspmu_map_cache_event(u64 config)
  576. {
  577. unsigned int cache_type, cache_op, cache_result;
  578. const struct mips_perf_event *pev;
  579. cache_type = (config >> 0) & 0xff;
  580. if (cache_type >= PERF_COUNT_HW_CACHE_MAX)
  581. return ERR_PTR(-EINVAL);
  582. cache_op = (config >> 8) & 0xff;
  583. if (cache_op >= PERF_COUNT_HW_CACHE_OP_MAX)
  584. return ERR_PTR(-EINVAL);
  585. cache_result = (config >> 16) & 0xff;
  586. if (cache_result >= PERF_COUNT_HW_CACHE_RESULT_MAX)
  587. return ERR_PTR(-EINVAL);
  588. pev = &((*mipspmu.cache_event_map)
  589. [cache_type]
  590. [cache_op]
  591. [cache_result]);
  592. if (pev->event_id == UNSUPPORTED_PERF_EVENT_ID)
  593. return ERR_PTR(-EOPNOTSUPP);
  594. return pev;
  595. }
  596. static int validate_event(struct cpu_hw_events *cpuc,
  597. struct perf_event *event)
  598. {
  599. struct hw_perf_event fake_hwc = event->hw;
  600. /* Allow mixed event group. So return 1 to pass validation. */
  601. if (event->pmu != &pmu || event->state <= PERF_EVENT_STATE_OFF)
  602. return 1;
  603. return mipsxx_pmu_alloc_counter(cpuc, &fake_hwc) >= 0;
  604. }
  605. static int validate_group(struct perf_event *event)
  606. {
  607. struct perf_event *sibling, *leader = event->group_leader;
  608. struct cpu_hw_events fake_cpuc;
  609. memset(&fake_cpuc, 0, sizeof(fake_cpuc));
  610. if (!validate_event(&fake_cpuc, leader))
  611. return -ENOSPC;
  612. list_for_each_entry(sibling, &leader->sibling_list, group_entry) {
  613. if (!validate_event(&fake_cpuc, sibling))
  614. return -ENOSPC;
  615. }
  616. if (!validate_event(&fake_cpuc, event))
  617. return -ENOSPC;
  618. return 0;
  619. }
  620. /* This is needed by specific irq handlers in perf_event_*.c */
  621. static void handle_associated_event(struct cpu_hw_events *cpuc,
  622. int idx, struct perf_sample_data *data,
  623. struct pt_regs *regs)
  624. {
  625. struct perf_event *event = cpuc->events[idx];
  626. struct hw_perf_event *hwc = &event->hw;
  627. mipspmu_event_update(event, hwc, idx);
  628. data->period = event->hw.last_period;
  629. if (!mipspmu_event_set_period(event, hwc, idx))
  630. return;
  631. if (perf_event_overflow(event, data, regs))
  632. mipsxx_pmu_disable_event(idx);
  633. }
  634. static int __n_counters(void)
  635. {
  636. if (!(read_c0_config1() & M_CONFIG1_PC))
  637. return 0;
  638. if (!(read_c0_perfctrl0() & M_PERFCTL_MORE))
  639. return 1;
  640. if (!(read_c0_perfctrl1() & M_PERFCTL_MORE))
  641. return 2;
  642. if (!(read_c0_perfctrl2() & M_PERFCTL_MORE))
  643. return 3;
  644. return 4;
  645. }
  646. static int n_counters(void)
  647. {
  648. int counters;
  649. switch (current_cpu_type()) {
  650. case CPU_R10000:
  651. counters = 2;
  652. break;
  653. case CPU_R12000:
  654. case CPU_R14000:
  655. counters = 4;
  656. break;
  657. default:
  658. counters = __n_counters();
  659. }
  660. return counters;
  661. }
  662. static void reset_counters(void *arg)
  663. {
  664. int counters = (int)(long)arg;
  665. switch (counters) {
  666. case 4:
  667. mipsxx_pmu_write_control(3, 0);
  668. mipspmu.write_counter(3, 0);
  669. case 3:
  670. mipsxx_pmu_write_control(2, 0);
  671. mipspmu.write_counter(2, 0);
  672. case 2:
  673. mipsxx_pmu_write_control(1, 0);
  674. mipspmu.write_counter(1, 0);
  675. case 1:
  676. mipsxx_pmu_write_control(0, 0);
  677. mipspmu.write_counter(0, 0);
  678. }
  679. }
  680. /* 24K/34K/1004K cores can share the same event map. */
  681. static const struct mips_perf_event mipsxxcore_event_map
  682. [PERF_COUNT_HW_MAX] = {
  683. [PERF_COUNT_HW_CPU_CYCLES] = { 0x00, CNTR_EVEN | CNTR_ODD, P },
  684. [PERF_COUNT_HW_INSTRUCTIONS] = { 0x01, CNTR_EVEN | CNTR_ODD, T },
  685. [PERF_COUNT_HW_CACHE_REFERENCES] = { UNSUPPORTED_PERF_EVENT_ID },
  686. [PERF_COUNT_HW_CACHE_MISSES] = { UNSUPPORTED_PERF_EVENT_ID },
  687. [PERF_COUNT_HW_BRANCH_INSTRUCTIONS] = { 0x02, CNTR_EVEN, T },
  688. [PERF_COUNT_HW_BRANCH_MISSES] = { 0x02, CNTR_ODD, T },
  689. [PERF_COUNT_HW_BUS_CYCLES] = { UNSUPPORTED_PERF_EVENT_ID },
  690. };
  691. /* 74K core has different branch event code. */
  692. static const struct mips_perf_event mipsxx74Kcore_event_map
  693. [PERF_COUNT_HW_MAX] = {
  694. [PERF_COUNT_HW_CPU_CYCLES] = { 0x00, CNTR_EVEN | CNTR_ODD, P },
  695. [PERF_COUNT_HW_INSTRUCTIONS] = { 0x01, CNTR_EVEN | CNTR_ODD, T },
  696. [PERF_COUNT_HW_CACHE_REFERENCES] = { UNSUPPORTED_PERF_EVENT_ID },
  697. [PERF_COUNT_HW_CACHE_MISSES] = { UNSUPPORTED_PERF_EVENT_ID },
  698. [PERF_COUNT_HW_BRANCH_INSTRUCTIONS] = { 0x27, CNTR_EVEN, T },
  699. [PERF_COUNT_HW_BRANCH_MISSES] = { 0x27, CNTR_ODD, T },
  700. [PERF_COUNT_HW_BUS_CYCLES] = { UNSUPPORTED_PERF_EVENT_ID },
  701. };
  702. static const struct mips_perf_event octeon_event_map[PERF_COUNT_HW_MAX] = {
  703. [PERF_COUNT_HW_CPU_CYCLES] = { 0x01, CNTR_ALL },
  704. [PERF_COUNT_HW_INSTRUCTIONS] = { 0x03, CNTR_ALL },
  705. [PERF_COUNT_HW_CACHE_REFERENCES] = { 0x2b, CNTR_ALL },
  706. [PERF_COUNT_HW_CACHE_MISSES] = { 0x2e, CNTR_ALL },
  707. [PERF_COUNT_HW_BRANCH_INSTRUCTIONS] = { 0x08, CNTR_ALL },
  708. [PERF_COUNT_HW_BRANCH_MISSES] = { 0x09, CNTR_ALL },
  709. [PERF_COUNT_HW_BUS_CYCLES] = { 0x25, CNTR_ALL },
  710. };
  711. /* 24K/34K/1004K cores can share the same cache event map. */
  712. static const struct mips_perf_event mipsxxcore_cache_map
  713. [PERF_COUNT_HW_CACHE_MAX]
  714. [PERF_COUNT_HW_CACHE_OP_MAX]
  715. [PERF_COUNT_HW_CACHE_RESULT_MAX] = {
  716. [C(L1D)] = {
  717. /*
  718. * Like some other architectures (e.g. ARM), the performance
  719. * counters don't differentiate between read and write
  720. * accesses/misses, so this isn't strictly correct, but it's the
  721. * best we can do. Writes and reads get combined.
  722. */
  723. [C(OP_READ)] = {
  724. [C(RESULT_ACCESS)] = { 0x0a, CNTR_EVEN, T },
  725. [C(RESULT_MISS)] = { 0x0b, CNTR_EVEN | CNTR_ODD, T },
  726. },
  727. [C(OP_WRITE)] = {
  728. [C(RESULT_ACCESS)] = { 0x0a, CNTR_EVEN, T },
  729. [C(RESULT_MISS)] = { 0x0b, CNTR_EVEN | CNTR_ODD, T },
  730. },
  731. [C(OP_PREFETCH)] = {
  732. [C(RESULT_ACCESS)] = { UNSUPPORTED_PERF_EVENT_ID },
  733. [C(RESULT_MISS)] = { UNSUPPORTED_PERF_EVENT_ID },
  734. },
  735. },
  736. [C(L1I)] = {
  737. [C(OP_READ)] = {
  738. [C(RESULT_ACCESS)] = { 0x09, CNTR_EVEN, T },
  739. [C(RESULT_MISS)] = { 0x09, CNTR_ODD, T },
  740. },
  741. [C(OP_WRITE)] = {
  742. [C(RESULT_ACCESS)] = { 0x09, CNTR_EVEN, T },
  743. [C(RESULT_MISS)] = { 0x09, CNTR_ODD, T },
  744. },
  745. [C(OP_PREFETCH)] = {
  746. [C(RESULT_ACCESS)] = { 0x14, CNTR_EVEN, T },
  747. /*
  748. * Note that MIPS has only "hit" events countable for
  749. * the prefetch operation.
  750. */
  751. [C(RESULT_MISS)] = { UNSUPPORTED_PERF_EVENT_ID },
  752. },
  753. },
  754. [C(LL)] = {
  755. [C(OP_READ)] = {
  756. [C(RESULT_ACCESS)] = { 0x15, CNTR_ODD, P },
  757. [C(RESULT_MISS)] = { 0x16, CNTR_EVEN, P },
  758. },
  759. [C(OP_WRITE)] = {
  760. [C(RESULT_ACCESS)] = { 0x15, CNTR_ODD, P },
  761. [C(RESULT_MISS)] = { 0x16, CNTR_EVEN, P },
  762. },
  763. [C(OP_PREFETCH)] = {
  764. [C(RESULT_ACCESS)] = { UNSUPPORTED_PERF_EVENT_ID },
  765. [C(RESULT_MISS)] = { UNSUPPORTED_PERF_EVENT_ID },
  766. },
  767. },
  768. [C(DTLB)] = {
  769. [C(OP_READ)] = {
  770. [C(RESULT_ACCESS)] = { 0x06, CNTR_EVEN, T },
  771. [C(RESULT_MISS)] = { 0x06, CNTR_ODD, T },
  772. },
  773. [C(OP_WRITE)] = {
  774. [C(RESULT_ACCESS)] = { 0x06, CNTR_EVEN, T },
  775. [C(RESULT_MISS)] = { 0x06, CNTR_ODD, T },
  776. },
  777. [C(OP_PREFETCH)] = {
  778. [C(RESULT_ACCESS)] = { UNSUPPORTED_PERF_EVENT_ID },
  779. [C(RESULT_MISS)] = { UNSUPPORTED_PERF_EVENT_ID },
  780. },
  781. },
  782. [C(ITLB)] = {
  783. [C(OP_READ)] = {
  784. [C(RESULT_ACCESS)] = { 0x05, CNTR_EVEN, T },
  785. [C(RESULT_MISS)] = { 0x05, CNTR_ODD, T },
  786. },
  787. [C(OP_WRITE)] = {
  788. [C(RESULT_ACCESS)] = { 0x05, CNTR_EVEN, T },
  789. [C(RESULT_MISS)] = { 0x05, CNTR_ODD, T },
  790. },
  791. [C(OP_PREFETCH)] = {
  792. [C(RESULT_ACCESS)] = { UNSUPPORTED_PERF_EVENT_ID },
  793. [C(RESULT_MISS)] = { UNSUPPORTED_PERF_EVENT_ID },
  794. },
  795. },
  796. [C(BPU)] = {
  797. /* Using the same code for *HW_BRANCH* */
  798. [C(OP_READ)] = {
  799. [C(RESULT_ACCESS)] = { 0x02, CNTR_EVEN, T },
  800. [C(RESULT_MISS)] = { 0x02, CNTR_ODD, T },
  801. },
  802. [C(OP_WRITE)] = {
  803. [C(RESULT_ACCESS)] = { 0x02, CNTR_EVEN, T },
  804. [C(RESULT_MISS)] = { 0x02, CNTR_ODD, T },
  805. },
  806. [C(OP_PREFETCH)] = {
  807. [C(RESULT_ACCESS)] = { UNSUPPORTED_PERF_EVENT_ID },
  808. [C(RESULT_MISS)] = { UNSUPPORTED_PERF_EVENT_ID },
  809. },
  810. },
  811. [C(NODE)] = {
  812. [C(OP_READ)] = {
  813. [C(RESULT_ACCESS)] = { UNSUPPORTED_PERF_EVENT_ID },
  814. [C(RESULT_MISS)] = { UNSUPPORTED_PERF_EVENT_ID },
  815. },
  816. [C(OP_WRITE)] = {
  817. [C(RESULT_ACCESS)] = { UNSUPPORTED_PERF_EVENT_ID },
  818. [C(RESULT_MISS)] = { UNSUPPORTED_PERF_EVENT_ID },
  819. },
  820. [C(OP_PREFETCH)] = {
  821. [C(RESULT_ACCESS)] = { UNSUPPORTED_PERF_EVENT_ID },
  822. [C(RESULT_MISS)] = { UNSUPPORTED_PERF_EVENT_ID },
  823. },
  824. },
  825. };
  826. /* 74K core has completely different cache event map. */
  827. static const struct mips_perf_event mipsxx74Kcore_cache_map
  828. [PERF_COUNT_HW_CACHE_MAX]
  829. [PERF_COUNT_HW_CACHE_OP_MAX]
  830. [PERF_COUNT_HW_CACHE_RESULT_MAX] = {
  831. [C(L1D)] = {
  832. /*
  833. * Like some other architectures (e.g. ARM), the performance
  834. * counters don't differentiate between read and write
  835. * accesses/misses, so this isn't strictly correct, but it's the
  836. * best we can do. Writes and reads get combined.
  837. */
  838. [C(OP_READ)] = {
  839. [C(RESULT_ACCESS)] = { 0x17, CNTR_ODD, T },
  840. [C(RESULT_MISS)] = { 0x18, CNTR_ODD, T },
  841. },
  842. [C(OP_WRITE)] = {
  843. [C(RESULT_ACCESS)] = { 0x17, CNTR_ODD, T },
  844. [C(RESULT_MISS)] = { 0x18, CNTR_ODD, T },
  845. },
  846. [C(OP_PREFETCH)] = {
  847. [C(RESULT_ACCESS)] = { UNSUPPORTED_PERF_EVENT_ID },
  848. [C(RESULT_MISS)] = { UNSUPPORTED_PERF_EVENT_ID },
  849. },
  850. },
  851. [C(L1I)] = {
  852. [C(OP_READ)] = {
  853. [C(RESULT_ACCESS)] = { 0x06, CNTR_EVEN, T },
  854. [C(RESULT_MISS)] = { 0x06, CNTR_ODD, T },
  855. },
  856. [C(OP_WRITE)] = {
  857. [C(RESULT_ACCESS)] = { 0x06, CNTR_EVEN, T },
  858. [C(RESULT_MISS)] = { 0x06, CNTR_ODD, T },
  859. },
  860. [C(OP_PREFETCH)] = {
  861. [C(RESULT_ACCESS)] = { 0x34, CNTR_EVEN, T },
  862. /*
  863. * Note that MIPS has only "hit" events countable for
  864. * the prefetch operation.
  865. */
  866. [C(RESULT_MISS)] = { UNSUPPORTED_PERF_EVENT_ID },
  867. },
  868. },
  869. [C(LL)] = {
  870. [C(OP_READ)] = {
  871. [C(RESULT_ACCESS)] = { 0x1c, CNTR_ODD, P },
  872. [C(RESULT_MISS)] = { 0x1d, CNTR_EVEN | CNTR_ODD, P },
  873. },
  874. [C(OP_WRITE)] = {
  875. [C(RESULT_ACCESS)] = { 0x1c, CNTR_ODD, P },
  876. [C(RESULT_MISS)] = { 0x1d, CNTR_EVEN | CNTR_ODD, P },
  877. },
  878. [C(OP_PREFETCH)] = {
  879. [C(RESULT_ACCESS)] = { UNSUPPORTED_PERF_EVENT_ID },
  880. [C(RESULT_MISS)] = { UNSUPPORTED_PERF_EVENT_ID },
  881. },
  882. },
  883. [C(DTLB)] = {
  884. /* 74K core does not have specific DTLB events. */
  885. [C(OP_READ)] = {
  886. [C(RESULT_ACCESS)] = { UNSUPPORTED_PERF_EVENT_ID },
  887. [C(RESULT_MISS)] = { UNSUPPORTED_PERF_EVENT_ID },
  888. },
  889. [C(OP_WRITE)] = {
  890. [C(RESULT_ACCESS)] = { UNSUPPORTED_PERF_EVENT_ID },
  891. [C(RESULT_MISS)] = { UNSUPPORTED_PERF_EVENT_ID },
  892. },
  893. [C(OP_PREFETCH)] = {
  894. [C(RESULT_ACCESS)] = { UNSUPPORTED_PERF_EVENT_ID },
  895. [C(RESULT_MISS)] = { UNSUPPORTED_PERF_EVENT_ID },
  896. },
  897. },
  898. [C(ITLB)] = {
  899. [C(OP_READ)] = {
  900. [C(RESULT_ACCESS)] = { 0x04, CNTR_EVEN, T },
  901. [C(RESULT_MISS)] = { 0x04, CNTR_ODD, T },
  902. },
  903. [C(OP_WRITE)] = {
  904. [C(RESULT_ACCESS)] = { 0x04, CNTR_EVEN, T },
  905. [C(RESULT_MISS)] = { 0x04, CNTR_ODD, T },
  906. },
  907. [C(OP_PREFETCH)] = {
  908. [C(RESULT_ACCESS)] = { UNSUPPORTED_PERF_EVENT_ID },
  909. [C(RESULT_MISS)] = { UNSUPPORTED_PERF_EVENT_ID },
  910. },
  911. },
  912. [C(BPU)] = {
  913. /* Using the same code for *HW_BRANCH* */
  914. [C(OP_READ)] = {
  915. [C(RESULT_ACCESS)] = { 0x27, CNTR_EVEN, T },
  916. [C(RESULT_MISS)] = { 0x27, CNTR_ODD, T },
  917. },
  918. [C(OP_WRITE)] = {
  919. [C(RESULT_ACCESS)] = { 0x27, CNTR_EVEN, T },
  920. [C(RESULT_MISS)] = { 0x27, CNTR_ODD, T },
  921. },
  922. [C(OP_PREFETCH)] = {
  923. [C(RESULT_ACCESS)] = { UNSUPPORTED_PERF_EVENT_ID },
  924. [C(RESULT_MISS)] = { UNSUPPORTED_PERF_EVENT_ID },
  925. },
  926. },
  927. [C(NODE)] = {
  928. [C(OP_READ)] = {
  929. [C(RESULT_ACCESS)] = { UNSUPPORTED_PERF_EVENT_ID },
  930. [C(RESULT_MISS)] = { UNSUPPORTED_PERF_EVENT_ID },
  931. },
  932. [C(OP_WRITE)] = {
  933. [C(RESULT_ACCESS)] = { UNSUPPORTED_PERF_EVENT_ID },
  934. [C(RESULT_MISS)] = { UNSUPPORTED_PERF_EVENT_ID },
  935. },
  936. [C(OP_PREFETCH)] = {
  937. [C(RESULT_ACCESS)] = { UNSUPPORTED_PERF_EVENT_ID },
  938. [C(RESULT_MISS)] = { UNSUPPORTED_PERF_EVENT_ID },
  939. },
  940. },
  941. };
  942. static const struct mips_perf_event octeon_cache_map
  943. [PERF_COUNT_HW_CACHE_MAX]
  944. [PERF_COUNT_HW_CACHE_OP_MAX]
  945. [PERF_COUNT_HW_CACHE_RESULT_MAX] = {
  946. [C(L1D)] = {
  947. [C(OP_READ)] = {
  948. [C(RESULT_ACCESS)] = { 0x2b, CNTR_ALL },
  949. [C(RESULT_MISS)] = { 0x2e, CNTR_ALL },
  950. },
  951. [C(OP_WRITE)] = {
  952. [C(RESULT_ACCESS)] = { 0x30, CNTR_ALL },
  953. [C(RESULT_MISS)] = { UNSUPPORTED_PERF_EVENT_ID },
  954. },
  955. [C(OP_PREFETCH)] = {
  956. [C(RESULT_ACCESS)] = { UNSUPPORTED_PERF_EVENT_ID },
  957. [C(RESULT_MISS)] = { UNSUPPORTED_PERF_EVENT_ID },
  958. },
  959. },
  960. [C(L1I)] = {
  961. [C(OP_READ)] = {
  962. [C(RESULT_ACCESS)] = { 0x18, CNTR_ALL },
  963. [C(RESULT_MISS)] = { UNSUPPORTED_PERF_EVENT_ID },
  964. },
  965. [C(OP_WRITE)] = {
  966. [C(RESULT_ACCESS)] = { UNSUPPORTED_PERF_EVENT_ID },
  967. [C(RESULT_MISS)] = { UNSUPPORTED_PERF_EVENT_ID },
  968. },
  969. [C(OP_PREFETCH)] = {
  970. [C(RESULT_ACCESS)] = { 0x19, CNTR_ALL },
  971. [C(RESULT_MISS)] = { UNSUPPORTED_PERF_EVENT_ID },
  972. },
  973. },
  974. [C(LL)] = {
  975. [C(OP_READ)] = {
  976. [C(RESULT_ACCESS)] = { UNSUPPORTED_PERF_EVENT_ID },
  977. [C(RESULT_MISS)] = { UNSUPPORTED_PERF_EVENT_ID },
  978. },
  979. [C(OP_WRITE)] = {
  980. [C(RESULT_ACCESS)] = { UNSUPPORTED_PERF_EVENT_ID },
  981. [C(RESULT_MISS)] = { UNSUPPORTED_PERF_EVENT_ID },
  982. },
  983. [C(OP_PREFETCH)] = {
  984. [C(RESULT_ACCESS)] = { UNSUPPORTED_PERF_EVENT_ID },
  985. [C(RESULT_MISS)] = { UNSUPPORTED_PERF_EVENT_ID },
  986. },
  987. },
  988. [C(DTLB)] = {
  989. /*
  990. * Only general DTLB misses are counted use the same event for
  991. * read and write.
  992. */
  993. [C(OP_READ)] = {
  994. [C(RESULT_ACCESS)] = { UNSUPPORTED_PERF_EVENT_ID },
  995. [C(RESULT_MISS)] = { 0x35, CNTR_ALL },
  996. },
  997. [C(OP_WRITE)] = {
  998. [C(RESULT_ACCESS)] = { UNSUPPORTED_PERF_EVENT_ID },
  999. [C(RESULT_MISS)] = { 0x35, CNTR_ALL },
  1000. },
  1001. [C(OP_PREFETCH)] = {
  1002. [C(RESULT_ACCESS)] = { UNSUPPORTED_PERF_EVENT_ID },
  1003. [C(RESULT_MISS)] = { UNSUPPORTED_PERF_EVENT_ID },
  1004. },
  1005. },
  1006. [C(ITLB)] = {
  1007. [C(OP_READ)] = {
  1008. [C(RESULT_ACCESS)] = { UNSUPPORTED_PERF_EVENT_ID },
  1009. [C(RESULT_MISS)] = { 0x37, CNTR_ALL },
  1010. },
  1011. [C(OP_WRITE)] = {
  1012. [C(RESULT_ACCESS)] = { UNSUPPORTED_PERF_EVENT_ID },
  1013. [C(RESULT_MISS)] = { UNSUPPORTED_PERF_EVENT_ID },
  1014. },
  1015. [C(OP_PREFETCH)] = {
  1016. [C(RESULT_ACCESS)] = { UNSUPPORTED_PERF_EVENT_ID },
  1017. [C(RESULT_MISS)] = { UNSUPPORTED_PERF_EVENT_ID },
  1018. },
  1019. },
  1020. [C(BPU)] = {
  1021. /* Using the same code for *HW_BRANCH* */
  1022. [C(OP_READ)] = {
  1023. [C(RESULT_ACCESS)] = { UNSUPPORTED_PERF_EVENT_ID },
  1024. [C(RESULT_MISS)] = { UNSUPPORTED_PERF_EVENT_ID },
  1025. },
  1026. [C(OP_WRITE)] = {
  1027. [C(RESULT_ACCESS)] = { UNSUPPORTED_PERF_EVENT_ID },
  1028. [C(RESULT_MISS)] = { UNSUPPORTED_PERF_EVENT_ID },
  1029. },
  1030. [C(OP_PREFETCH)] = {
  1031. [C(RESULT_ACCESS)] = { UNSUPPORTED_PERF_EVENT_ID },
  1032. [C(RESULT_MISS)] = { UNSUPPORTED_PERF_EVENT_ID },
  1033. },
  1034. },
  1035. };
  1036. #ifdef CONFIG_MIPS_MT_SMP
  1037. static void check_and_calc_range(struct perf_event *event,
  1038. const struct mips_perf_event *pev)
  1039. {
  1040. struct hw_perf_event *hwc = &event->hw;
  1041. if (event->cpu >= 0) {
  1042. if (pev->range > V) {
  1043. /*
  1044. * The user selected an event that is processor
  1045. * wide, while expecting it to be VPE wide.
  1046. */
  1047. hwc->config_base |= M_TC_EN_ALL;
  1048. } else {
  1049. /*
  1050. * FIXME: cpu_data[event->cpu].vpe_id reports 0
  1051. * for both CPUs.
  1052. */
  1053. hwc->config_base |= M_PERFCTL_VPEID(event->cpu);
  1054. hwc->config_base |= M_TC_EN_VPE;
  1055. }
  1056. } else
  1057. hwc->config_base |= M_TC_EN_ALL;
  1058. }
  1059. #else
  1060. static void check_and_calc_range(struct perf_event *event,
  1061. const struct mips_perf_event *pev)
  1062. {
  1063. }
  1064. #endif
  1065. static int __hw_perf_event_init(struct perf_event *event)
  1066. {
  1067. struct perf_event_attr *attr = &event->attr;
  1068. struct hw_perf_event *hwc = &event->hw;
  1069. const struct mips_perf_event *pev;
  1070. int err;
  1071. /* Returning MIPS event descriptor for generic perf event. */
  1072. if (PERF_TYPE_HARDWARE == event->attr.type) {
  1073. if (event->attr.config >= PERF_COUNT_HW_MAX)
  1074. return -EINVAL;
  1075. pev = mipspmu_map_general_event(event->attr.config);
  1076. } else if (PERF_TYPE_HW_CACHE == event->attr.type) {
  1077. pev = mipspmu_map_cache_event(event->attr.config);
  1078. } else if (PERF_TYPE_RAW == event->attr.type) {
  1079. /* We are working on the global raw event. */
  1080. mutex_lock(&raw_event_mutex);
  1081. pev = mipspmu.map_raw_event(event->attr.config);
  1082. } else {
  1083. /* The event type is not (yet) supported. */
  1084. return -EOPNOTSUPP;
  1085. }
  1086. if (IS_ERR(pev)) {
  1087. if (PERF_TYPE_RAW == event->attr.type)
  1088. mutex_unlock(&raw_event_mutex);
  1089. return PTR_ERR(pev);
  1090. }
  1091. /*
  1092. * We allow max flexibility on how each individual counter shared
  1093. * by the single CPU operates (the mode exclusion and the range).
  1094. */
  1095. hwc->config_base = M_PERFCTL_INTERRUPT_ENABLE;
  1096. /* Calculate range bits and validate it. */
  1097. if (num_possible_cpus() > 1)
  1098. check_and_calc_range(event, pev);
  1099. hwc->event_base = mipspmu_perf_event_encode(pev);
  1100. if (PERF_TYPE_RAW == event->attr.type)
  1101. mutex_unlock(&raw_event_mutex);
  1102. if (!attr->exclude_user)
  1103. hwc->config_base |= M_PERFCTL_USER;
  1104. if (!attr->exclude_kernel) {
  1105. hwc->config_base |= M_PERFCTL_KERNEL;
  1106. /* MIPS kernel mode: KSU == 00b || EXL == 1 || ERL == 1 */
  1107. hwc->config_base |= M_PERFCTL_EXL;
  1108. }
  1109. if (!attr->exclude_hv)
  1110. hwc->config_base |= M_PERFCTL_SUPERVISOR;
  1111. hwc->config_base &= M_PERFCTL_CONFIG_MASK;
  1112. /*
  1113. * The event can belong to another cpu. We do not assign a local
  1114. * counter for it for now.
  1115. */
  1116. hwc->idx = -1;
  1117. hwc->config = 0;
  1118. if (!hwc->sample_period) {
  1119. hwc->sample_period = mipspmu.max_period;
  1120. hwc->last_period = hwc->sample_period;
  1121. local64_set(&hwc->period_left, hwc->sample_period);
  1122. }
  1123. err = 0;
  1124. if (event->group_leader != event) {
  1125. err = validate_group(event);
  1126. if (err)
  1127. return -EINVAL;
  1128. }
  1129. event->destroy = hw_perf_event_destroy;
  1130. return err;
  1131. }
  1132. static void pause_local_counters(void)
  1133. {
  1134. struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
  1135. int ctr = mipspmu.num_counters;
  1136. unsigned long flags;
  1137. local_irq_save(flags);
  1138. do {
  1139. ctr--;
  1140. cpuc->saved_ctrl[ctr] = mipsxx_pmu_read_control(ctr);
  1141. mipsxx_pmu_write_control(ctr, cpuc->saved_ctrl[ctr] &
  1142. ~M_PERFCTL_COUNT_EVENT_WHENEVER);
  1143. } while (ctr > 0);
  1144. local_irq_restore(flags);
  1145. }
  1146. static void resume_local_counters(void)
  1147. {
  1148. struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
  1149. int ctr = mipspmu.num_counters;
  1150. do {
  1151. ctr--;
  1152. mipsxx_pmu_write_control(ctr, cpuc->saved_ctrl[ctr]);
  1153. } while (ctr > 0);
  1154. }
  1155. static int mipsxx_pmu_handle_shared_irq(void)
  1156. {
  1157. struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
  1158. struct perf_sample_data data;
  1159. unsigned int counters = mipspmu.num_counters;
  1160. u64 counter;
  1161. int handled = IRQ_NONE;
  1162. struct pt_regs *regs;
  1163. if (cpu_has_mips_r2 && !(read_c0_cause() & (1 << 26)))
  1164. return handled;
  1165. /*
  1166. * First we pause the local counters, so that when we are locked
  1167. * here, the counters are all paused. When it gets locked due to
  1168. * perf_disable(), the timer interrupt handler will be delayed.
  1169. *
  1170. * See also mipsxx_pmu_start().
  1171. */
  1172. pause_local_counters();
  1173. #ifdef CONFIG_MIPS_MT_SMP
  1174. read_lock(&pmuint_rwlock);
  1175. #endif
  1176. regs = get_irq_regs();
  1177. perf_sample_data_init(&data, 0);
  1178. switch (counters) {
  1179. #define HANDLE_COUNTER(n) \
  1180. case n + 1: \
  1181. if (test_bit(n, cpuc->used_mask)) { \
  1182. counter = mipspmu.read_counter(n); \
  1183. if (counter & mipspmu.overflow) { \
  1184. handle_associated_event(cpuc, n, &data, regs); \
  1185. handled = IRQ_HANDLED; \
  1186. } \
  1187. }
  1188. HANDLE_COUNTER(3)
  1189. HANDLE_COUNTER(2)
  1190. HANDLE_COUNTER(1)
  1191. HANDLE_COUNTER(0)
  1192. }
  1193. /*
  1194. * Do all the work for the pending perf events. We can do this
  1195. * in here because the performance counter interrupt is a regular
  1196. * interrupt, not NMI.
  1197. */
  1198. if (handled == IRQ_HANDLED)
  1199. irq_work_run();
  1200. #ifdef CONFIG_MIPS_MT_SMP
  1201. read_unlock(&pmuint_rwlock);
  1202. #endif
  1203. resume_local_counters();
  1204. return handled;
  1205. }
  1206. static irqreturn_t mipsxx_pmu_handle_irq(int irq, void *dev)
  1207. {
  1208. return mipsxx_pmu_handle_shared_irq();
  1209. }
  1210. /* 24K */
  1211. #define IS_BOTH_COUNTERS_24K_EVENT(b) \
  1212. ((b) == 0 || (b) == 1 || (b) == 11)
  1213. /* 34K */
  1214. #define IS_BOTH_COUNTERS_34K_EVENT(b) \
  1215. ((b) == 0 || (b) == 1 || (b) == 11)
  1216. #ifdef CONFIG_MIPS_MT_SMP
  1217. #define IS_RANGE_P_34K_EVENT(r, b) \
  1218. ((b) == 0 || (r) == 18 || (b) == 21 || (b) == 22 || \
  1219. (b) == 25 || (b) == 39 || (r) == 44 || (r) == 174 || \
  1220. (r) == 176 || ((b) >= 50 && (b) <= 55) || \
  1221. ((b) >= 64 && (b) <= 67))
  1222. #define IS_RANGE_V_34K_EVENT(r) ((r) == 47)
  1223. #endif
  1224. /* 74K */
  1225. #define IS_BOTH_COUNTERS_74K_EVENT(b) \
  1226. ((b) == 0 || (b) == 1)
  1227. /* 1004K */
  1228. #define IS_BOTH_COUNTERS_1004K_EVENT(b) \
  1229. ((b) == 0 || (b) == 1 || (b) == 11)
  1230. #ifdef CONFIG_MIPS_MT_SMP
  1231. #define IS_RANGE_P_1004K_EVENT(r, b) \
  1232. ((b) == 0 || (r) == 18 || (b) == 21 || (b) == 22 || \
  1233. (b) == 25 || (b) == 36 || (b) == 39 || (r) == 44 || \
  1234. (r) == 174 || (r) == 176 || ((b) >= 50 && (b) <= 59) || \
  1235. (r) == 188 || (b) == 61 || (b) == 62 || \
  1236. ((b) >= 64 && (b) <= 67))
  1237. #define IS_RANGE_V_1004K_EVENT(r) ((r) == 47)
  1238. #endif
  1239. /*
  1240. * User can use 0-255 raw events, where 0-127 for the events of even
  1241. * counters, and 128-255 for odd counters. Note that bit 7 is used to
  1242. * indicate the parity. So, for example, when user wants to take the
  1243. * Event Num of 15 for odd counters (by referring to the user manual),
  1244. * then 128 needs to be added to 15 as the input for the event config,
  1245. * i.e., 143 (0x8F) to be used.
  1246. */
  1247. static const struct mips_perf_event *mipsxx_pmu_map_raw_event(u64 config)
  1248. {
  1249. unsigned int raw_id = config & 0xff;
  1250. unsigned int base_id = raw_id & 0x7f;
  1251. raw_event.event_id = base_id;
  1252. switch (current_cpu_type()) {
  1253. case CPU_24K:
  1254. if (IS_BOTH_COUNTERS_24K_EVENT(base_id))
  1255. raw_event.cntr_mask = CNTR_EVEN | CNTR_ODD;
  1256. else
  1257. raw_event.cntr_mask =
  1258. raw_id > 127 ? CNTR_ODD : CNTR_EVEN;
  1259. #ifdef CONFIG_MIPS_MT_SMP
  1260. /*
  1261. * This is actually doing nothing. Non-multithreading
  1262. * CPUs will not check and calculate the range.
  1263. */
  1264. raw_event.range = P;
  1265. #endif
  1266. break;
  1267. case CPU_34K:
  1268. if (IS_BOTH_COUNTERS_34K_EVENT(base_id))
  1269. raw_event.cntr_mask = CNTR_EVEN | CNTR_ODD;
  1270. else
  1271. raw_event.cntr_mask =
  1272. raw_id > 127 ? CNTR_ODD : CNTR_EVEN;
  1273. #ifdef CONFIG_MIPS_MT_SMP
  1274. if (IS_RANGE_P_34K_EVENT(raw_id, base_id))
  1275. raw_event.range = P;
  1276. else if (unlikely(IS_RANGE_V_34K_EVENT(raw_id)))
  1277. raw_event.range = V;
  1278. else
  1279. raw_event.range = T;
  1280. #endif
  1281. break;
  1282. case CPU_74K:
  1283. if (IS_BOTH_COUNTERS_74K_EVENT(base_id))
  1284. raw_event.cntr_mask = CNTR_EVEN | CNTR_ODD;
  1285. else
  1286. raw_event.cntr_mask =
  1287. raw_id > 127 ? CNTR_ODD : CNTR_EVEN;
  1288. #ifdef CONFIG_MIPS_MT_SMP
  1289. raw_event.range = P;
  1290. #endif
  1291. break;
  1292. case CPU_1004K:
  1293. if (IS_BOTH_COUNTERS_1004K_EVENT(base_id))
  1294. raw_event.cntr_mask = CNTR_EVEN | CNTR_ODD;
  1295. else
  1296. raw_event.cntr_mask =
  1297. raw_id > 127 ? CNTR_ODD : CNTR_EVEN;
  1298. #ifdef CONFIG_MIPS_MT_SMP
  1299. if (IS_RANGE_P_1004K_EVENT(raw_id, base_id))
  1300. raw_event.range = P;
  1301. else if (unlikely(IS_RANGE_V_1004K_EVENT(raw_id)))
  1302. raw_event.range = V;
  1303. else
  1304. raw_event.range = T;
  1305. #endif
  1306. break;
  1307. }
  1308. return &raw_event;
  1309. }
  1310. static const struct mips_perf_event *octeon_pmu_map_raw_event(u64 config)
  1311. {
  1312. unsigned int raw_id = config & 0xff;
  1313. unsigned int base_id = raw_id & 0x7f;
  1314. raw_event.cntr_mask = CNTR_ALL;
  1315. raw_event.event_id = base_id;
  1316. if (current_cpu_type() == CPU_CAVIUM_OCTEON2) {
  1317. if (base_id > 0x42)
  1318. return ERR_PTR(-EOPNOTSUPP);
  1319. } else {
  1320. if (base_id > 0x3a)
  1321. return ERR_PTR(-EOPNOTSUPP);
  1322. }
  1323. switch (base_id) {
  1324. case 0x00:
  1325. case 0x0f:
  1326. case 0x1e:
  1327. case 0x1f:
  1328. case 0x2f:
  1329. case 0x34:
  1330. case 0x3b ... 0x3f:
  1331. return ERR_PTR(-EOPNOTSUPP);
  1332. default:
  1333. break;
  1334. }
  1335. return &raw_event;
  1336. }
  1337. static int __init
  1338. init_hw_perf_events(void)
  1339. {
  1340. int counters, irq;
  1341. int counter_bits;
  1342. pr_info("Performance counters: ");
  1343. counters = n_counters();
  1344. if (counters == 0) {
  1345. pr_cont("No available PMU.\n");
  1346. return -ENODEV;
  1347. }
  1348. #ifdef CONFIG_MIPS_MT_SMP
  1349. cpu_has_mipsmt_pertccounters = read_c0_config7() & (1<<19);
  1350. if (!cpu_has_mipsmt_pertccounters)
  1351. counters = counters_total_to_per_cpu(counters);
  1352. #endif
  1353. #ifdef MSC01E_INT_BASE
  1354. if (cpu_has_veic) {
  1355. /*
  1356. * Using platform specific interrupt controller defines.
  1357. */
  1358. irq = MSC01E_INT_BASE + MSC01E_INT_PERFCTR;
  1359. } else {
  1360. #endif
  1361. if (cp0_perfcount_irq >= 0)
  1362. irq = MIPS_CPU_IRQ_BASE + cp0_perfcount_irq;
  1363. else
  1364. irq = -1;
  1365. #ifdef MSC01E_INT_BASE
  1366. }
  1367. #endif
  1368. mipspmu.map_raw_event = mipsxx_pmu_map_raw_event;
  1369. switch (current_cpu_type()) {
  1370. case CPU_24K:
  1371. mipspmu.name = "mips/24K";
  1372. mipspmu.general_event_map = &mipsxxcore_event_map;
  1373. mipspmu.cache_event_map = &mipsxxcore_cache_map;
  1374. break;
  1375. case CPU_34K:
  1376. mipspmu.name = "mips/34K";
  1377. mipspmu.general_event_map = &mipsxxcore_event_map;
  1378. mipspmu.cache_event_map = &mipsxxcore_cache_map;
  1379. break;
  1380. case CPU_74K:
  1381. mipspmu.name = "mips/74K";
  1382. mipspmu.general_event_map = &mipsxx74Kcore_event_map;
  1383. mipspmu.cache_event_map = &mipsxx74Kcore_cache_map;
  1384. break;
  1385. case CPU_1004K:
  1386. mipspmu.name = "mips/1004K";
  1387. mipspmu.general_event_map = &mipsxxcore_event_map;
  1388. mipspmu.cache_event_map = &mipsxxcore_cache_map;
  1389. break;
  1390. case CPU_CAVIUM_OCTEON:
  1391. case CPU_CAVIUM_OCTEON_PLUS:
  1392. case CPU_CAVIUM_OCTEON2:
  1393. mipspmu.name = "octeon";
  1394. mipspmu.general_event_map = &octeon_event_map;
  1395. mipspmu.cache_event_map = &octeon_cache_map;
  1396. mipspmu.map_raw_event = octeon_pmu_map_raw_event;
  1397. break;
  1398. default:
  1399. pr_cont("Either hardware does not support performance "
  1400. "counters, or not yet implemented.\n");
  1401. return -ENODEV;
  1402. }
  1403. mipspmu.num_counters = counters;
  1404. mipspmu.irq = irq;
  1405. if (read_c0_perfctrl0() & M_PERFCTL_WIDE) {
  1406. mipspmu.max_period = (1ULL << 63) - 1;
  1407. mipspmu.valid_count = (1ULL << 63) - 1;
  1408. mipspmu.overflow = 1ULL << 63;
  1409. mipspmu.read_counter = mipsxx_pmu_read_counter_64;
  1410. mipspmu.write_counter = mipsxx_pmu_write_counter_64;
  1411. counter_bits = 64;
  1412. } else {
  1413. mipspmu.max_period = (1ULL << 31) - 1;
  1414. mipspmu.valid_count = (1ULL << 31) - 1;
  1415. mipspmu.overflow = 1ULL << 31;
  1416. mipspmu.read_counter = mipsxx_pmu_read_counter;
  1417. mipspmu.write_counter = mipsxx_pmu_write_counter;
  1418. counter_bits = 32;
  1419. }
  1420. on_each_cpu(reset_counters, (void *)(long)counters, 1);
  1421. pr_cont("%s PMU enabled, %d %d-bit counters available to each "
  1422. "CPU, irq %d%s\n", mipspmu.name, counters, counter_bits, irq,
  1423. irq < 0 ? " (share with timer interrupt)" : "");
  1424. perf_pmu_register(&pmu, "cpu", PERF_TYPE_RAW);
  1425. return 0;
  1426. }
  1427. early_initcall(init_hw_perf_events);