perf_event_mipsxx.c 44 KB

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