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