perf_event_mipsxx.c 41 KB

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