perf_event_mipsxx.c 36 KB

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