perf_event_mipsxx.c 46 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805
  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 |
  473. IRQF_NO_THREAD | IRQF_NO_SUSPEND |
  474. IRQF_SHARED,
  475. "mips_perf_pmu", &mipspmu);
  476. if (err) {
  477. pr_warn("Unable to request IRQ%d for MIPS performance counters!\n",
  478. mipspmu.irq);
  479. }
  480. } else if (cp0_perfcount_irq < 0) {
  481. /*
  482. * We are sharing the irq number with the timer interrupt.
  483. */
  484. save_perf_irq = perf_irq;
  485. perf_irq = mipsxx_pmu_handle_shared_irq;
  486. err = 0;
  487. } else {
  488. pr_warn("The platform hasn't properly defined its interrupt controller\n");
  489. err = -ENOENT;
  490. }
  491. return err;
  492. }
  493. static void mipspmu_free_irq(void)
  494. {
  495. if (mipspmu.irq >= 0)
  496. free_irq(mipspmu.irq, &mipspmu);
  497. else if (cp0_perfcount_irq < 0)
  498. perf_irq = save_perf_irq;
  499. }
  500. /*
  501. * mipsxx/rm9000/loongson2 have different performance counters, they have
  502. * specific low-level init routines.
  503. */
  504. static void reset_counters(void *arg);
  505. static int __hw_perf_event_init(struct perf_event *event);
  506. static void hw_perf_event_destroy(struct perf_event *event)
  507. {
  508. if (atomic_dec_and_mutex_lock(&active_events,
  509. &pmu_reserve_mutex)) {
  510. /*
  511. * We must not call the destroy function with interrupts
  512. * disabled.
  513. */
  514. on_each_cpu(reset_counters,
  515. (void *)(long)mipspmu.num_counters, 1);
  516. mipspmu_free_irq();
  517. mutex_unlock(&pmu_reserve_mutex);
  518. }
  519. }
  520. static int mipspmu_event_init(struct perf_event *event)
  521. {
  522. int err = 0;
  523. /* does not support taken branch sampling */
  524. if (has_branch_stack(event))
  525. return -EOPNOTSUPP;
  526. switch (event->attr.type) {
  527. case PERF_TYPE_RAW:
  528. case PERF_TYPE_HARDWARE:
  529. case PERF_TYPE_HW_CACHE:
  530. break;
  531. default:
  532. return -ENOENT;
  533. }
  534. if (event->cpu >= nr_cpumask_bits ||
  535. (event->cpu >= 0 && !cpu_online(event->cpu)))
  536. return -ENODEV;
  537. if (!atomic_inc_not_zero(&active_events)) {
  538. mutex_lock(&pmu_reserve_mutex);
  539. if (atomic_read(&active_events) == 0)
  540. err = mipspmu_get_irq();
  541. if (!err)
  542. atomic_inc(&active_events);
  543. mutex_unlock(&pmu_reserve_mutex);
  544. }
  545. if (err)
  546. return err;
  547. return __hw_perf_event_init(event);
  548. }
  549. static struct pmu pmu = {
  550. .pmu_enable = mipspmu_enable,
  551. .pmu_disable = mipspmu_disable,
  552. .event_init = mipspmu_event_init,
  553. .add = mipspmu_add,
  554. .del = mipspmu_del,
  555. .start = mipspmu_start,
  556. .stop = mipspmu_stop,
  557. .read = mipspmu_read,
  558. };
  559. static unsigned int mipspmu_perf_event_encode(const struct mips_perf_event *pev)
  560. {
  561. /*
  562. * Top 8 bits for range, next 16 bits for cntr_mask, lowest 8 bits for
  563. * event_id.
  564. */
  565. #ifdef CONFIG_MIPS_MT_SMP
  566. return ((unsigned int)pev->range << 24) |
  567. (pev->cntr_mask & 0xffff00) |
  568. (pev->event_id & 0xff);
  569. #else
  570. return (pev->cntr_mask & 0xffff00) |
  571. (pev->event_id & 0xff);
  572. #endif
  573. }
  574. static const struct mips_perf_event *mipspmu_map_general_event(int idx)
  575. {
  576. if ((*mipspmu.general_event_map)[idx].cntr_mask == 0)
  577. return ERR_PTR(-EOPNOTSUPP);
  578. return &(*mipspmu.general_event_map)[idx];
  579. }
  580. static const struct mips_perf_event *mipspmu_map_cache_event(u64 config)
  581. {
  582. unsigned int cache_type, cache_op, cache_result;
  583. const struct mips_perf_event *pev;
  584. cache_type = (config >> 0) & 0xff;
  585. if (cache_type >= PERF_COUNT_HW_CACHE_MAX)
  586. return ERR_PTR(-EINVAL);
  587. cache_op = (config >> 8) & 0xff;
  588. if (cache_op >= PERF_COUNT_HW_CACHE_OP_MAX)
  589. return ERR_PTR(-EINVAL);
  590. cache_result = (config >> 16) & 0xff;
  591. if (cache_result >= PERF_COUNT_HW_CACHE_RESULT_MAX)
  592. return ERR_PTR(-EINVAL);
  593. pev = &((*mipspmu.cache_event_map)
  594. [cache_type]
  595. [cache_op]
  596. [cache_result]);
  597. if (pev->cntr_mask == 0)
  598. return ERR_PTR(-EOPNOTSUPP);
  599. return pev;
  600. }
  601. static int validate_group(struct perf_event *event)
  602. {
  603. struct perf_event *sibling, *leader = event->group_leader;
  604. struct cpu_hw_events fake_cpuc;
  605. memset(&fake_cpuc, 0, sizeof(fake_cpuc));
  606. if (mipsxx_pmu_alloc_counter(&fake_cpuc, &leader->hw) < 0)
  607. return -EINVAL;
  608. list_for_each_entry(sibling, &leader->sibling_list, group_entry) {
  609. if (mipsxx_pmu_alloc_counter(&fake_cpuc, &sibling->hw) < 0)
  610. return -EINVAL;
  611. }
  612. if (mipsxx_pmu_alloc_counter(&fake_cpuc, &event->hw) < 0)
  613. return -EINVAL;
  614. return 0;
  615. }
  616. /* This is needed by specific irq handlers in perf_event_*.c */
  617. static void handle_associated_event(struct cpu_hw_events *cpuc,
  618. int idx, struct perf_sample_data *data,
  619. struct pt_regs *regs)
  620. {
  621. struct perf_event *event = cpuc->events[idx];
  622. struct hw_perf_event *hwc = &event->hw;
  623. mipspmu_event_update(event, hwc, idx);
  624. data->period = event->hw.last_period;
  625. if (!mipspmu_event_set_period(event, hwc, idx))
  626. return;
  627. if (perf_event_overflow(event, data, regs))
  628. mipsxx_pmu_disable_event(idx);
  629. }
  630. static int __n_counters(void)
  631. {
  632. if (!(read_c0_config1() & M_CONFIG1_PC))
  633. return 0;
  634. if (!(read_c0_perfctrl0() & M_PERFCTL_MORE))
  635. return 1;
  636. if (!(read_c0_perfctrl1() & M_PERFCTL_MORE))
  637. return 2;
  638. if (!(read_c0_perfctrl2() & M_PERFCTL_MORE))
  639. return 3;
  640. return 4;
  641. }
  642. static int n_counters(void)
  643. {
  644. int counters;
  645. switch (current_cpu_type()) {
  646. case CPU_R10000:
  647. counters = 2;
  648. break;
  649. case CPU_R12000:
  650. case CPU_R14000:
  651. case CPU_R16000:
  652. counters = 4;
  653. break;
  654. default:
  655. counters = __n_counters();
  656. }
  657. return counters;
  658. }
  659. static void reset_counters(void *arg)
  660. {
  661. int counters = (int)(long)arg;
  662. switch (counters) {
  663. case 4:
  664. mipsxx_pmu_write_control(3, 0);
  665. mipspmu.write_counter(3, 0);
  666. case 3:
  667. mipsxx_pmu_write_control(2, 0);
  668. mipspmu.write_counter(2, 0);
  669. case 2:
  670. mipsxx_pmu_write_control(1, 0);
  671. mipspmu.write_counter(1, 0);
  672. case 1:
  673. mipsxx_pmu_write_control(0, 0);
  674. mipspmu.write_counter(0, 0);
  675. }
  676. }
  677. /* 24K/34K/1004K/interAptiv/loongson1 cores share the same event map. */
  678. static const struct mips_perf_event mipsxxcore_event_map
  679. [PERF_COUNT_HW_MAX] = {
  680. [PERF_COUNT_HW_CPU_CYCLES] = { 0x00, CNTR_EVEN | CNTR_ODD, P },
  681. [PERF_COUNT_HW_INSTRUCTIONS] = { 0x01, CNTR_EVEN | CNTR_ODD, T },
  682. [PERF_COUNT_HW_BRANCH_INSTRUCTIONS] = { 0x02, CNTR_EVEN, T },
  683. [PERF_COUNT_HW_BRANCH_MISSES] = { 0x02, CNTR_ODD, T },
  684. };
  685. /* 74K/proAptiv core has different branch event code. */
  686. static const struct mips_perf_event mipsxxcore_event_map2
  687. [PERF_COUNT_HW_MAX] = {
  688. [PERF_COUNT_HW_CPU_CYCLES] = { 0x00, CNTR_EVEN | CNTR_ODD, P },
  689. [PERF_COUNT_HW_INSTRUCTIONS] = { 0x01, CNTR_EVEN | CNTR_ODD, T },
  690. [PERF_COUNT_HW_BRANCH_INSTRUCTIONS] = { 0x27, CNTR_EVEN, T },
  691. [PERF_COUNT_HW_BRANCH_MISSES] = { 0x27, CNTR_ODD, T },
  692. };
  693. static const struct mips_perf_event loongson3_event_map[PERF_COUNT_HW_MAX] = {
  694. [PERF_COUNT_HW_CPU_CYCLES] = { 0x00, CNTR_EVEN },
  695. [PERF_COUNT_HW_INSTRUCTIONS] = { 0x00, CNTR_ODD },
  696. [PERF_COUNT_HW_BRANCH_INSTRUCTIONS] = { 0x01, CNTR_EVEN },
  697. [PERF_COUNT_HW_BRANCH_MISSES] = { 0x01, CNTR_ODD },
  698. };
  699. static const struct mips_perf_event octeon_event_map[PERF_COUNT_HW_MAX] = {
  700. [PERF_COUNT_HW_CPU_CYCLES] = { 0x01, CNTR_ALL },
  701. [PERF_COUNT_HW_INSTRUCTIONS] = { 0x03, CNTR_ALL },
  702. [PERF_COUNT_HW_CACHE_REFERENCES] = { 0x2b, CNTR_ALL },
  703. [PERF_COUNT_HW_CACHE_MISSES] = { 0x2e, CNTR_ALL },
  704. [PERF_COUNT_HW_BRANCH_INSTRUCTIONS] = { 0x08, CNTR_ALL },
  705. [PERF_COUNT_HW_BRANCH_MISSES] = { 0x09, CNTR_ALL },
  706. [PERF_COUNT_HW_BUS_CYCLES] = { 0x25, CNTR_ALL },
  707. };
  708. static const struct mips_perf_event bmips5000_event_map
  709. [PERF_COUNT_HW_MAX] = {
  710. [PERF_COUNT_HW_CPU_CYCLES] = { 0x00, CNTR_EVEN | CNTR_ODD, T },
  711. [PERF_COUNT_HW_INSTRUCTIONS] = { 0x01, CNTR_EVEN | CNTR_ODD, T },
  712. [PERF_COUNT_HW_BRANCH_MISSES] = { 0x02, CNTR_ODD, T },
  713. };
  714. static const struct mips_perf_event xlp_event_map[PERF_COUNT_HW_MAX] = {
  715. [PERF_COUNT_HW_CPU_CYCLES] = { 0x01, CNTR_ALL },
  716. [PERF_COUNT_HW_INSTRUCTIONS] = { 0x18, CNTR_ALL }, /* PAPI_TOT_INS */
  717. [PERF_COUNT_HW_CACHE_REFERENCES] = { 0x04, CNTR_ALL }, /* PAPI_L1_ICA */
  718. [PERF_COUNT_HW_CACHE_MISSES] = { 0x07, CNTR_ALL }, /* PAPI_L1_ICM */
  719. [PERF_COUNT_HW_BRANCH_INSTRUCTIONS] = { 0x1b, CNTR_ALL }, /* PAPI_BR_CN */
  720. [PERF_COUNT_HW_BRANCH_MISSES] = { 0x1c, CNTR_ALL }, /* PAPI_BR_MSP */
  721. };
  722. /* 24K/34K/1004K/interAptiv/loongson1 cores share the same cache event map. */
  723. static const struct mips_perf_event mipsxxcore_cache_map
  724. [PERF_COUNT_HW_CACHE_MAX]
  725. [PERF_COUNT_HW_CACHE_OP_MAX]
  726. [PERF_COUNT_HW_CACHE_RESULT_MAX] = {
  727. [C(L1D)] = {
  728. /*
  729. * Like some other architectures (e.g. ARM), the performance
  730. * counters don't differentiate between read and write
  731. * accesses/misses, so this isn't strictly correct, but it's the
  732. * best we can do. Writes and reads get combined.
  733. */
  734. [C(OP_READ)] = {
  735. [C(RESULT_ACCESS)] = { 0x0a, CNTR_EVEN, T },
  736. [C(RESULT_MISS)] = { 0x0b, CNTR_EVEN | CNTR_ODD, T },
  737. },
  738. [C(OP_WRITE)] = {
  739. [C(RESULT_ACCESS)] = { 0x0a, CNTR_EVEN, T },
  740. [C(RESULT_MISS)] = { 0x0b, CNTR_EVEN | CNTR_ODD, T },
  741. },
  742. },
  743. [C(L1I)] = {
  744. [C(OP_READ)] = {
  745. [C(RESULT_ACCESS)] = { 0x09, CNTR_EVEN, T },
  746. [C(RESULT_MISS)] = { 0x09, CNTR_ODD, T },
  747. },
  748. [C(OP_WRITE)] = {
  749. [C(RESULT_ACCESS)] = { 0x09, CNTR_EVEN, T },
  750. [C(RESULT_MISS)] = { 0x09, CNTR_ODD, T },
  751. },
  752. [C(OP_PREFETCH)] = {
  753. [C(RESULT_ACCESS)] = { 0x14, CNTR_EVEN, T },
  754. /*
  755. * Note that MIPS has only "hit" events countable for
  756. * the prefetch operation.
  757. */
  758. },
  759. },
  760. [C(LL)] = {
  761. [C(OP_READ)] = {
  762. [C(RESULT_ACCESS)] = { 0x15, CNTR_ODD, P },
  763. [C(RESULT_MISS)] = { 0x16, CNTR_EVEN, P },
  764. },
  765. [C(OP_WRITE)] = {
  766. [C(RESULT_ACCESS)] = { 0x15, CNTR_ODD, P },
  767. [C(RESULT_MISS)] = { 0x16, CNTR_EVEN, P },
  768. },
  769. },
  770. [C(DTLB)] = {
  771. [C(OP_READ)] = {
  772. [C(RESULT_ACCESS)] = { 0x06, CNTR_EVEN, T },
  773. [C(RESULT_MISS)] = { 0x06, CNTR_ODD, T },
  774. },
  775. [C(OP_WRITE)] = {
  776. [C(RESULT_ACCESS)] = { 0x06, CNTR_EVEN, T },
  777. [C(RESULT_MISS)] = { 0x06, CNTR_ODD, T },
  778. },
  779. },
  780. [C(ITLB)] = {
  781. [C(OP_READ)] = {
  782. [C(RESULT_ACCESS)] = { 0x05, CNTR_EVEN, T },
  783. [C(RESULT_MISS)] = { 0x05, CNTR_ODD, T },
  784. },
  785. [C(OP_WRITE)] = {
  786. [C(RESULT_ACCESS)] = { 0x05, CNTR_EVEN, T },
  787. [C(RESULT_MISS)] = { 0x05, CNTR_ODD, T },
  788. },
  789. },
  790. [C(BPU)] = {
  791. /* Using the same code for *HW_BRANCH* */
  792. [C(OP_READ)] = {
  793. [C(RESULT_ACCESS)] = { 0x02, CNTR_EVEN, T },
  794. [C(RESULT_MISS)] = { 0x02, CNTR_ODD, T },
  795. },
  796. [C(OP_WRITE)] = {
  797. [C(RESULT_ACCESS)] = { 0x02, CNTR_EVEN, T },
  798. [C(RESULT_MISS)] = { 0x02, CNTR_ODD, T },
  799. },
  800. },
  801. };
  802. /* 74K/proAptiv core has completely different cache event map. */
  803. static const struct mips_perf_event mipsxxcore_cache_map2
  804. [PERF_COUNT_HW_CACHE_MAX]
  805. [PERF_COUNT_HW_CACHE_OP_MAX]
  806. [PERF_COUNT_HW_CACHE_RESULT_MAX] = {
  807. [C(L1D)] = {
  808. /*
  809. * Like some other architectures (e.g. ARM), the performance
  810. * counters don't differentiate between read and write
  811. * accesses/misses, so this isn't strictly correct, but it's the
  812. * best we can do. Writes and reads get combined.
  813. */
  814. [C(OP_READ)] = {
  815. [C(RESULT_ACCESS)] = { 0x17, CNTR_ODD, T },
  816. [C(RESULT_MISS)] = { 0x18, CNTR_ODD, T },
  817. },
  818. [C(OP_WRITE)] = {
  819. [C(RESULT_ACCESS)] = { 0x17, CNTR_ODD, T },
  820. [C(RESULT_MISS)] = { 0x18, CNTR_ODD, T },
  821. },
  822. },
  823. [C(L1I)] = {
  824. [C(OP_READ)] = {
  825. [C(RESULT_ACCESS)] = { 0x06, CNTR_EVEN, T },
  826. [C(RESULT_MISS)] = { 0x06, CNTR_ODD, T },
  827. },
  828. [C(OP_WRITE)] = {
  829. [C(RESULT_ACCESS)] = { 0x06, CNTR_EVEN, T },
  830. [C(RESULT_MISS)] = { 0x06, CNTR_ODD, T },
  831. },
  832. [C(OP_PREFETCH)] = {
  833. [C(RESULT_ACCESS)] = { 0x34, CNTR_EVEN, T },
  834. /*
  835. * Note that MIPS has only "hit" events countable for
  836. * the prefetch operation.
  837. */
  838. },
  839. },
  840. [C(LL)] = {
  841. [C(OP_READ)] = {
  842. [C(RESULT_ACCESS)] = { 0x1c, CNTR_ODD, P },
  843. [C(RESULT_MISS)] = { 0x1d, CNTR_EVEN, P },
  844. },
  845. [C(OP_WRITE)] = {
  846. [C(RESULT_ACCESS)] = { 0x1c, CNTR_ODD, P },
  847. [C(RESULT_MISS)] = { 0x1d, CNTR_EVEN, P },
  848. },
  849. },
  850. /*
  851. * 74K core does not have specific DTLB events. proAptiv core has
  852. * "speculative" DTLB events which are numbered 0x63 (even/odd) and
  853. * not included here. One can use raw events if really needed.
  854. */
  855. [C(ITLB)] = {
  856. [C(OP_READ)] = {
  857. [C(RESULT_ACCESS)] = { 0x04, CNTR_EVEN, T },
  858. [C(RESULT_MISS)] = { 0x04, CNTR_ODD, T },
  859. },
  860. [C(OP_WRITE)] = {
  861. [C(RESULT_ACCESS)] = { 0x04, CNTR_EVEN, T },
  862. [C(RESULT_MISS)] = { 0x04, CNTR_ODD, T },
  863. },
  864. },
  865. [C(BPU)] = {
  866. /* Using the same code for *HW_BRANCH* */
  867. [C(OP_READ)] = {
  868. [C(RESULT_ACCESS)] = { 0x27, CNTR_EVEN, T },
  869. [C(RESULT_MISS)] = { 0x27, CNTR_ODD, T },
  870. },
  871. [C(OP_WRITE)] = {
  872. [C(RESULT_ACCESS)] = { 0x27, CNTR_EVEN, T },
  873. [C(RESULT_MISS)] = { 0x27, CNTR_ODD, T },
  874. },
  875. },
  876. };
  877. static const struct mips_perf_event loongson3_cache_map
  878. [PERF_COUNT_HW_CACHE_MAX]
  879. [PERF_COUNT_HW_CACHE_OP_MAX]
  880. [PERF_COUNT_HW_CACHE_RESULT_MAX] = {
  881. [C(L1D)] = {
  882. /*
  883. * Like some other architectures (e.g. ARM), the performance
  884. * counters don't differentiate between read and write
  885. * accesses/misses, so this isn't strictly correct, but it's the
  886. * best we can do. Writes and reads get combined.
  887. */
  888. [C(OP_READ)] = {
  889. [C(RESULT_MISS)] = { 0x04, CNTR_ODD },
  890. },
  891. [C(OP_WRITE)] = {
  892. [C(RESULT_MISS)] = { 0x04, CNTR_ODD },
  893. },
  894. },
  895. [C(L1I)] = {
  896. [C(OP_READ)] = {
  897. [C(RESULT_MISS)] = { 0x04, CNTR_EVEN },
  898. },
  899. [C(OP_WRITE)] = {
  900. [C(RESULT_MISS)] = { 0x04, CNTR_EVEN },
  901. },
  902. },
  903. [C(DTLB)] = {
  904. [C(OP_READ)] = {
  905. [C(RESULT_MISS)] = { 0x09, CNTR_ODD },
  906. },
  907. [C(OP_WRITE)] = {
  908. [C(RESULT_MISS)] = { 0x09, CNTR_ODD },
  909. },
  910. },
  911. [C(ITLB)] = {
  912. [C(OP_READ)] = {
  913. [C(RESULT_MISS)] = { 0x0c, CNTR_ODD },
  914. },
  915. [C(OP_WRITE)] = {
  916. [C(RESULT_MISS)] = { 0x0c, CNTR_ODD },
  917. },
  918. },
  919. [C(BPU)] = {
  920. /* Using the same code for *HW_BRANCH* */
  921. [C(OP_READ)] = {
  922. [C(RESULT_ACCESS)] = { 0x02, CNTR_EVEN },
  923. [C(RESULT_MISS)] = { 0x02, CNTR_ODD },
  924. },
  925. [C(OP_WRITE)] = {
  926. [C(RESULT_ACCESS)] = { 0x02, CNTR_EVEN },
  927. [C(RESULT_MISS)] = { 0x02, CNTR_ODD },
  928. },
  929. },
  930. };
  931. /* BMIPS5000 */
  932. static const struct mips_perf_event bmips5000_cache_map
  933. [PERF_COUNT_HW_CACHE_MAX]
  934. [PERF_COUNT_HW_CACHE_OP_MAX]
  935. [PERF_COUNT_HW_CACHE_RESULT_MAX] = {
  936. [C(L1D)] = {
  937. /*
  938. * Like some other architectures (e.g. ARM), the performance
  939. * counters don't differentiate between read and write
  940. * accesses/misses, so this isn't strictly correct, but it's the
  941. * best we can do. Writes and reads get combined.
  942. */
  943. [C(OP_READ)] = {
  944. [C(RESULT_ACCESS)] = { 12, CNTR_EVEN, T },
  945. [C(RESULT_MISS)] = { 12, CNTR_ODD, T },
  946. },
  947. [C(OP_WRITE)] = {
  948. [C(RESULT_ACCESS)] = { 12, CNTR_EVEN, T },
  949. [C(RESULT_MISS)] = { 12, CNTR_ODD, T },
  950. },
  951. },
  952. [C(L1I)] = {
  953. [C(OP_READ)] = {
  954. [C(RESULT_ACCESS)] = { 10, CNTR_EVEN, T },
  955. [C(RESULT_MISS)] = { 10, CNTR_ODD, T },
  956. },
  957. [C(OP_WRITE)] = {
  958. [C(RESULT_ACCESS)] = { 10, CNTR_EVEN, T },
  959. [C(RESULT_MISS)] = { 10, CNTR_ODD, T },
  960. },
  961. [C(OP_PREFETCH)] = {
  962. [C(RESULT_ACCESS)] = { 23, CNTR_EVEN, T },
  963. /*
  964. * Note that MIPS has only "hit" events countable for
  965. * the prefetch operation.
  966. */
  967. },
  968. },
  969. [C(LL)] = {
  970. [C(OP_READ)] = {
  971. [C(RESULT_ACCESS)] = { 28, CNTR_EVEN, P },
  972. [C(RESULT_MISS)] = { 28, CNTR_ODD, P },
  973. },
  974. [C(OP_WRITE)] = {
  975. [C(RESULT_ACCESS)] = { 28, CNTR_EVEN, P },
  976. [C(RESULT_MISS)] = { 28, CNTR_ODD, P },
  977. },
  978. },
  979. [C(BPU)] = {
  980. /* Using the same code for *HW_BRANCH* */
  981. [C(OP_READ)] = {
  982. [C(RESULT_MISS)] = { 0x02, CNTR_ODD, T },
  983. },
  984. [C(OP_WRITE)] = {
  985. [C(RESULT_MISS)] = { 0x02, CNTR_ODD, T },
  986. },
  987. },
  988. };
  989. static const struct mips_perf_event octeon_cache_map
  990. [PERF_COUNT_HW_CACHE_MAX]
  991. [PERF_COUNT_HW_CACHE_OP_MAX]
  992. [PERF_COUNT_HW_CACHE_RESULT_MAX] = {
  993. [C(L1D)] = {
  994. [C(OP_READ)] = {
  995. [C(RESULT_ACCESS)] = { 0x2b, CNTR_ALL },
  996. [C(RESULT_MISS)] = { 0x2e, CNTR_ALL },
  997. },
  998. [C(OP_WRITE)] = {
  999. [C(RESULT_ACCESS)] = { 0x30, CNTR_ALL },
  1000. },
  1001. },
  1002. [C(L1I)] = {
  1003. [C(OP_READ)] = {
  1004. [C(RESULT_ACCESS)] = { 0x18, CNTR_ALL },
  1005. },
  1006. [C(OP_PREFETCH)] = {
  1007. [C(RESULT_ACCESS)] = { 0x19, CNTR_ALL },
  1008. },
  1009. },
  1010. [C(DTLB)] = {
  1011. /*
  1012. * Only general DTLB misses are counted use the same event for
  1013. * read and write.
  1014. */
  1015. [C(OP_READ)] = {
  1016. [C(RESULT_MISS)] = { 0x35, CNTR_ALL },
  1017. },
  1018. [C(OP_WRITE)] = {
  1019. [C(RESULT_MISS)] = { 0x35, CNTR_ALL },
  1020. },
  1021. },
  1022. [C(ITLB)] = {
  1023. [C(OP_READ)] = {
  1024. [C(RESULT_MISS)] = { 0x37, CNTR_ALL },
  1025. },
  1026. },
  1027. };
  1028. static const struct mips_perf_event xlp_cache_map
  1029. [PERF_COUNT_HW_CACHE_MAX]
  1030. [PERF_COUNT_HW_CACHE_OP_MAX]
  1031. [PERF_COUNT_HW_CACHE_RESULT_MAX] = {
  1032. [C(L1D)] = {
  1033. [C(OP_READ)] = {
  1034. [C(RESULT_ACCESS)] = { 0x31, CNTR_ALL }, /* PAPI_L1_DCR */
  1035. [C(RESULT_MISS)] = { 0x30, CNTR_ALL }, /* PAPI_L1_LDM */
  1036. },
  1037. [C(OP_WRITE)] = {
  1038. [C(RESULT_ACCESS)] = { 0x2f, CNTR_ALL }, /* PAPI_L1_DCW */
  1039. [C(RESULT_MISS)] = { 0x2e, CNTR_ALL }, /* PAPI_L1_STM */
  1040. },
  1041. },
  1042. [C(L1I)] = {
  1043. [C(OP_READ)] = {
  1044. [C(RESULT_ACCESS)] = { 0x04, CNTR_ALL }, /* PAPI_L1_ICA */
  1045. [C(RESULT_MISS)] = { 0x07, CNTR_ALL }, /* PAPI_L1_ICM */
  1046. },
  1047. },
  1048. [C(LL)] = {
  1049. [C(OP_READ)] = {
  1050. [C(RESULT_ACCESS)] = { 0x35, CNTR_ALL }, /* PAPI_L2_DCR */
  1051. [C(RESULT_MISS)] = { 0x37, CNTR_ALL }, /* PAPI_L2_LDM */
  1052. },
  1053. [C(OP_WRITE)] = {
  1054. [C(RESULT_ACCESS)] = { 0x34, CNTR_ALL }, /* PAPI_L2_DCA */
  1055. [C(RESULT_MISS)] = { 0x36, CNTR_ALL }, /* PAPI_L2_DCM */
  1056. },
  1057. },
  1058. [C(DTLB)] = {
  1059. /*
  1060. * Only general DTLB misses are counted use the same event for
  1061. * read and write.
  1062. */
  1063. [C(OP_READ)] = {
  1064. [C(RESULT_MISS)] = { 0x2d, CNTR_ALL }, /* PAPI_TLB_DM */
  1065. },
  1066. [C(OP_WRITE)] = {
  1067. [C(RESULT_MISS)] = { 0x2d, CNTR_ALL }, /* PAPI_TLB_DM */
  1068. },
  1069. },
  1070. [C(ITLB)] = {
  1071. [C(OP_READ)] = {
  1072. [C(RESULT_MISS)] = { 0x08, CNTR_ALL }, /* PAPI_TLB_IM */
  1073. },
  1074. [C(OP_WRITE)] = {
  1075. [C(RESULT_MISS)] = { 0x08, CNTR_ALL }, /* PAPI_TLB_IM */
  1076. },
  1077. },
  1078. [C(BPU)] = {
  1079. [C(OP_READ)] = {
  1080. [C(RESULT_MISS)] = { 0x25, CNTR_ALL },
  1081. },
  1082. },
  1083. };
  1084. #ifdef CONFIG_MIPS_MT_SMP
  1085. static void check_and_calc_range(struct perf_event *event,
  1086. const struct mips_perf_event *pev)
  1087. {
  1088. struct hw_perf_event *hwc = &event->hw;
  1089. if (event->cpu >= 0) {
  1090. if (pev->range > V) {
  1091. /*
  1092. * The user selected an event that is processor
  1093. * wide, while expecting it to be VPE wide.
  1094. */
  1095. hwc->config_base |= M_TC_EN_ALL;
  1096. } else {
  1097. /*
  1098. * FIXME: cpu_data[event->cpu].vpe_id reports 0
  1099. * for both CPUs.
  1100. */
  1101. hwc->config_base |= M_PERFCTL_VPEID(event->cpu);
  1102. hwc->config_base |= M_TC_EN_VPE;
  1103. }
  1104. } else
  1105. hwc->config_base |= M_TC_EN_ALL;
  1106. }
  1107. #else
  1108. static void check_and_calc_range(struct perf_event *event,
  1109. const struct mips_perf_event *pev)
  1110. {
  1111. }
  1112. #endif
  1113. static int __hw_perf_event_init(struct perf_event *event)
  1114. {
  1115. struct perf_event_attr *attr = &event->attr;
  1116. struct hw_perf_event *hwc = &event->hw;
  1117. const struct mips_perf_event *pev;
  1118. int err;
  1119. /* Returning MIPS event descriptor for generic perf event. */
  1120. if (PERF_TYPE_HARDWARE == event->attr.type) {
  1121. if (event->attr.config >= PERF_COUNT_HW_MAX)
  1122. return -EINVAL;
  1123. pev = mipspmu_map_general_event(event->attr.config);
  1124. } else if (PERF_TYPE_HW_CACHE == event->attr.type) {
  1125. pev = mipspmu_map_cache_event(event->attr.config);
  1126. } else if (PERF_TYPE_RAW == event->attr.type) {
  1127. /* We are working on the global raw event. */
  1128. mutex_lock(&raw_event_mutex);
  1129. pev = mipspmu.map_raw_event(event->attr.config);
  1130. } else {
  1131. /* The event type is not (yet) supported. */
  1132. return -EOPNOTSUPP;
  1133. }
  1134. if (IS_ERR(pev)) {
  1135. if (PERF_TYPE_RAW == event->attr.type)
  1136. mutex_unlock(&raw_event_mutex);
  1137. return PTR_ERR(pev);
  1138. }
  1139. /*
  1140. * We allow max flexibility on how each individual counter shared
  1141. * by the single CPU operates (the mode exclusion and the range).
  1142. */
  1143. hwc->config_base = M_PERFCTL_INTERRUPT_ENABLE;
  1144. /* Calculate range bits and validate it. */
  1145. if (num_possible_cpus() > 1)
  1146. check_and_calc_range(event, pev);
  1147. hwc->event_base = mipspmu_perf_event_encode(pev);
  1148. if (PERF_TYPE_RAW == event->attr.type)
  1149. mutex_unlock(&raw_event_mutex);
  1150. if (!attr->exclude_user)
  1151. hwc->config_base |= M_PERFCTL_USER;
  1152. if (!attr->exclude_kernel) {
  1153. hwc->config_base |= M_PERFCTL_KERNEL;
  1154. /* MIPS kernel mode: KSU == 00b || EXL == 1 || ERL == 1 */
  1155. hwc->config_base |= M_PERFCTL_EXL;
  1156. }
  1157. if (!attr->exclude_hv)
  1158. hwc->config_base |= M_PERFCTL_SUPERVISOR;
  1159. hwc->config_base &= M_PERFCTL_CONFIG_MASK;
  1160. /*
  1161. * The event can belong to another cpu. We do not assign a local
  1162. * counter for it for now.
  1163. */
  1164. hwc->idx = -1;
  1165. hwc->config = 0;
  1166. if (!hwc->sample_period) {
  1167. hwc->sample_period = mipspmu.max_period;
  1168. hwc->last_period = hwc->sample_period;
  1169. local64_set(&hwc->period_left, hwc->sample_period);
  1170. }
  1171. err = 0;
  1172. if (event->group_leader != event)
  1173. err = validate_group(event);
  1174. event->destroy = hw_perf_event_destroy;
  1175. if (err)
  1176. event->destroy(event);
  1177. return err;
  1178. }
  1179. static void pause_local_counters(void)
  1180. {
  1181. struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
  1182. int ctr = mipspmu.num_counters;
  1183. unsigned long flags;
  1184. local_irq_save(flags);
  1185. do {
  1186. ctr--;
  1187. cpuc->saved_ctrl[ctr] = mipsxx_pmu_read_control(ctr);
  1188. mipsxx_pmu_write_control(ctr, cpuc->saved_ctrl[ctr] &
  1189. ~M_PERFCTL_COUNT_EVENT_WHENEVER);
  1190. } while (ctr > 0);
  1191. local_irq_restore(flags);
  1192. }
  1193. static void resume_local_counters(void)
  1194. {
  1195. struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
  1196. int ctr = mipspmu.num_counters;
  1197. do {
  1198. ctr--;
  1199. mipsxx_pmu_write_control(ctr, cpuc->saved_ctrl[ctr]);
  1200. } while (ctr > 0);
  1201. }
  1202. static int mipsxx_pmu_handle_shared_irq(void)
  1203. {
  1204. struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
  1205. struct perf_sample_data data;
  1206. unsigned int counters = mipspmu.num_counters;
  1207. u64 counter;
  1208. int handled = IRQ_NONE;
  1209. struct pt_regs *regs;
  1210. if (cpu_has_perf_cntr_intr_bit && !(read_c0_cause() & CAUSEF_PCI))
  1211. return handled;
  1212. /*
  1213. * First we pause the local counters, so that when we are locked
  1214. * here, the counters are all paused. When it gets locked due to
  1215. * perf_disable(), the timer interrupt handler will be delayed.
  1216. *
  1217. * See also mipsxx_pmu_start().
  1218. */
  1219. pause_local_counters();
  1220. #ifdef CONFIG_MIPS_PERF_SHARED_TC_COUNTERS
  1221. read_lock(&pmuint_rwlock);
  1222. #endif
  1223. regs = get_irq_regs();
  1224. perf_sample_data_init(&data, 0, 0);
  1225. switch (counters) {
  1226. #define HANDLE_COUNTER(n) \
  1227. case n + 1: \
  1228. if (test_bit(n, cpuc->used_mask)) { \
  1229. counter = mipspmu.read_counter(n); \
  1230. if (counter & mipspmu.overflow) { \
  1231. handle_associated_event(cpuc, n, &data, regs); \
  1232. handled = IRQ_HANDLED; \
  1233. } \
  1234. }
  1235. HANDLE_COUNTER(3)
  1236. HANDLE_COUNTER(2)
  1237. HANDLE_COUNTER(1)
  1238. HANDLE_COUNTER(0)
  1239. }
  1240. /*
  1241. * Do all the work for the pending perf events. We can do this
  1242. * in here because the performance counter interrupt is a regular
  1243. * interrupt, not NMI.
  1244. */
  1245. if (handled == IRQ_HANDLED)
  1246. irq_work_run();
  1247. #ifdef CONFIG_MIPS_PERF_SHARED_TC_COUNTERS
  1248. read_unlock(&pmuint_rwlock);
  1249. #endif
  1250. resume_local_counters();
  1251. return handled;
  1252. }
  1253. static irqreturn_t mipsxx_pmu_handle_irq(int irq, void *dev)
  1254. {
  1255. return mipsxx_pmu_handle_shared_irq();
  1256. }
  1257. /* 24K */
  1258. #define IS_BOTH_COUNTERS_24K_EVENT(b) \
  1259. ((b) == 0 || (b) == 1 || (b) == 11)
  1260. /* 34K */
  1261. #define IS_BOTH_COUNTERS_34K_EVENT(b) \
  1262. ((b) == 0 || (b) == 1 || (b) == 11)
  1263. #ifdef CONFIG_MIPS_MT_SMP
  1264. #define IS_RANGE_P_34K_EVENT(r, b) \
  1265. ((b) == 0 || (r) == 18 || (b) == 21 || (b) == 22 || \
  1266. (b) == 25 || (b) == 39 || (r) == 44 || (r) == 174 || \
  1267. (r) == 176 || ((b) >= 50 && (b) <= 55) || \
  1268. ((b) >= 64 && (b) <= 67))
  1269. #define IS_RANGE_V_34K_EVENT(r) ((r) == 47)
  1270. #endif
  1271. /* 74K */
  1272. #define IS_BOTH_COUNTERS_74K_EVENT(b) \
  1273. ((b) == 0 || (b) == 1)
  1274. /* proAptiv */
  1275. #define IS_BOTH_COUNTERS_PROAPTIV_EVENT(b) \
  1276. ((b) == 0 || (b) == 1)
  1277. /* P5600 */
  1278. #define IS_BOTH_COUNTERS_P5600_EVENT(b) \
  1279. ((b) == 0 || (b) == 1)
  1280. /* 1004K */
  1281. #define IS_BOTH_COUNTERS_1004K_EVENT(b) \
  1282. ((b) == 0 || (b) == 1 || (b) == 11)
  1283. #ifdef CONFIG_MIPS_MT_SMP
  1284. #define IS_RANGE_P_1004K_EVENT(r, b) \
  1285. ((b) == 0 || (r) == 18 || (b) == 21 || (b) == 22 || \
  1286. (b) == 25 || (b) == 36 || (b) == 39 || (r) == 44 || \
  1287. (r) == 174 || (r) == 176 || ((b) >= 50 && (b) <= 59) || \
  1288. (r) == 188 || (b) == 61 || (b) == 62 || \
  1289. ((b) >= 64 && (b) <= 67))
  1290. #define IS_RANGE_V_1004K_EVENT(r) ((r) == 47)
  1291. #endif
  1292. /* interAptiv */
  1293. #define IS_BOTH_COUNTERS_INTERAPTIV_EVENT(b) \
  1294. ((b) == 0 || (b) == 1 || (b) == 11)
  1295. #ifdef CONFIG_MIPS_MT_SMP
  1296. /* The P/V/T info is not provided for "(b) == 38" in SUM, assume P. */
  1297. #define IS_RANGE_P_INTERAPTIV_EVENT(r, b) \
  1298. ((b) == 0 || (r) == 18 || (b) == 21 || (b) == 22 || \
  1299. (b) == 25 || (b) == 36 || (b) == 38 || (b) == 39 || \
  1300. (r) == 44 || (r) == 174 || (r) == 176 || ((b) >= 50 && \
  1301. (b) <= 59) || (r) == 188 || (b) == 61 || (b) == 62 || \
  1302. ((b) >= 64 && (b) <= 67))
  1303. #define IS_RANGE_V_INTERAPTIV_EVENT(r) ((r) == 47 || (r) == 175)
  1304. #endif
  1305. /* BMIPS5000 */
  1306. #define IS_BOTH_COUNTERS_BMIPS5000_EVENT(b) \
  1307. ((b) == 0 || (b) == 1)
  1308. /*
  1309. * For most cores the user can use 0-255 raw events, where 0-127 for the events
  1310. * of even counters, and 128-255 for odd counters. Note that bit 7 is used to
  1311. * indicate the even/odd bank selector. So, for example, when user wants to take
  1312. * the Event Num of 15 for odd counters (by referring to the user manual), then
  1313. * 128 needs to be added to 15 as the input for the event config, i.e., 143 (0x8F)
  1314. * to be used.
  1315. *
  1316. * Some newer cores have even more events, in which case the user can use raw
  1317. * events 0-511, where 0-255 are for the events of even counters, and 256-511
  1318. * are for odd counters, so bit 8 is used to indicate the even/odd bank selector.
  1319. */
  1320. static const struct mips_perf_event *mipsxx_pmu_map_raw_event(u64 config)
  1321. {
  1322. /* currently most cores have 7-bit event numbers */
  1323. unsigned int raw_id = config & 0xff;
  1324. unsigned int base_id = raw_id & 0x7f;
  1325. switch (current_cpu_type()) {
  1326. case CPU_24K:
  1327. if (IS_BOTH_COUNTERS_24K_EVENT(base_id))
  1328. raw_event.cntr_mask = CNTR_EVEN | CNTR_ODD;
  1329. else
  1330. raw_event.cntr_mask =
  1331. raw_id > 127 ? CNTR_ODD : CNTR_EVEN;
  1332. #ifdef CONFIG_MIPS_MT_SMP
  1333. /*
  1334. * This is actually doing nothing. Non-multithreading
  1335. * CPUs will not check and calculate the range.
  1336. */
  1337. raw_event.range = P;
  1338. #endif
  1339. break;
  1340. case CPU_34K:
  1341. if (IS_BOTH_COUNTERS_34K_EVENT(base_id))
  1342. raw_event.cntr_mask = CNTR_EVEN | CNTR_ODD;
  1343. else
  1344. raw_event.cntr_mask =
  1345. raw_id > 127 ? CNTR_ODD : CNTR_EVEN;
  1346. #ifdef CONFIG_MIPS_MT_SMP
  1347. if (IS_RANGE_P_34K_EVENT(raw_id, base_id))
  1348. raw_event.range = P;
  1349. else if (unlikely(IS_RANGE_V_34K_EVENT(raw_id)))
  1350. raw_event.range = V;
  1351. else
  1352. raw_event.range = T;
  1353. #endif
  1354. break;
  1355. case CPU_74K:
  1356. case CPU_1074K:
  1357. if (IS_BOTH_COUNTERS_74K_EVENT(base_id))
  1358. raw_event.cntr_mask = CNTR_EVEN | CNTR_ODD;
  1359. else
  1360. raw_event.cntr_mask =
  1361. raw_id > 127 ? CNTR_ODD : CNTR_EVEN;
  1362. #ifdef CONFIG_MIPS_MT_SMP
  1363. raw_event.range = P;
  1364. #endif
  1365. break;
  1366. case CPU_PROAPTIV:
  1367. if (IS_BOTH_COUNTERS_PROAPTIV_EVENT(base_id))
  1368. raw_event.cntr_mask = CNTR_EVEN | CNTR_ODD;
  1369. else
  1370. raw_event.cntr_mask =
  1371. raw_id > 127 ? CNTR_ODD : CNTR_EVEN;
  1372. #ifdef CONFIG_MIPS_MT_SMP
  1373. raw_event.range = P;
  1374. #endif
  1375. break;
  1376. case CPU_P5600:
  1377. case CPU_I6400:
  1378. /* 8-bit event numbers */
  1379. raw_id = config & 0x1ff;
  1380. base_id = raw_id & 0xff;
  1381. if (IS_BOTH_COUNTERS_P5600_EVENT(base_id))
  1382. raw_event.cntr_mask = CNTR_EVEN | CNTR_ODD;
  1383. else
  1384. raw_event.cntr_mask =
  1385. raw_id > 255 ? CNTR_ODD : CNTR_EVEN;
  1386. #ifdef CONFIG_MIPS_MT_SMP
  1387. raw_event.range = P;
  1388. #endif
  1389. break;
  1390. case CPU_1004K:
  1391. if (IS_BOTH_COUNTERS_1004K_EVENT(base_id))
  1392. raw_event.cntr_mask = CNTR_EVEN | CNTR_ODD;
  1393. else
  1394. raw_event.cntr_mask =
  1395. raw_id > 127 ? CNTR_ODD : CNTR_EVEN;
  1396. #ifdef CONFIG_MIPS_MT_SMP
  1397. if (IS_RANGE_P_1004K_EVENT(raw_id, base_id))
  1398. raw_event.range = P;
  1399. else if (unlikely(IS_RANGE_V_1004K_EVENT(raw_id)))
  1400. raw_event.range = V;
  1401. else
  1402. raw_event.range = T;
  1403. #endif
  1404. break;
  1405. case CPU_INTERAPTIV:
  1406. if (IS_BOTH_COUNTERS_INTERAPTIV_EVENT(base_id))
  1407. raw_event.cntr_mask = CNTR_EVEN | CNTR_ODD;
  1408. else
  1409. raw_event.cntr_mask =
  1410. raw_id > 127 ? CNTR_ODD : CNTR_EVEN;
  1411. #ifdef CONFIG_MIPS_MT_SMP
  1412. if (IS_RANGE_P_INTERAPTIV_EVENT(raw_id, base_id))
  1413. raw_event.range = P;
  1414. else if (unlikely(IS_RANGE_V_INTERAPTIV_EVENT(raw_id)))
  1415. raw_event.range = V;
  1416. else
  1417. raw_event.range = T;
  1418. #endif
  1419. break;
  1420. case CPU_BMIPS5000:
  1421. if (IS_BOTH_COUNTERS_BMIPS5000_EVENT(base_id))
  1422. raw_event.cntr_mask = CNTR_EVEN | CNTR_ODD;
  1423. else
  1424. raw_event.cntr_mask =
  1425. raw_id > 127 ? CNTR_ODD : CNTR_EVEN;
  1426. break;
  1427. case CPU_LOONGSON3:
  1428. raw_event.cntr_mask = raw_id > 127 ? CNTR_ODD : CNTR_EVEN;
  1429. break;
  1430. }
  1431. raw_event.event_id = base_id;
  1432. return &raw_event;
  1433. }
  1434. static const struct mips_perf_event *octeon_pmu_map_raw_event(u64 config)
  1435. {
  1436. unsigned int raw_id = config & 0xff;
  1437. unsigned int base_id = raw_id & 0x7f;
  1438. raw_event.cntr_mask = CNTR_ALL;
  1439. raw_event.event_id = base_id;
  1440. if (current_cpu_type() == CPU_CAVIUM_OCTEON2) {
  1441. if (base_id > 0x42)
  1442. return ERR_PTR(-EOPNOTSUPP);
  1443. } else {
  1444. if (base_id > 0x3a)
  1445. return ERR_PTR(-EOPNOTSUPP);
  1446. }
  1447. switch (base_id) {
  1448. case 0x00:
  1449. case 0x0f:
  1450. case 0x1e:
  1451. case 0x1f:
  1452. case 0x2f:
  1453. case 0x34:
  1454. case 0x3b ... 0x3f:
  1455. return ERR_PTR(-EOPNOTSUPP);
  1456. default:
  1457. break;
  1458. }
  1459. return &raw_event;
  1460. }
  1461. static const struct mips_perf_event *xlp_pmu_map_raw_event(u64 config)
  1462. {
  1463. unsigned int raw_id = config & 0xff;
  1464. /* Only 1-63 are defined */
  1465. if ((raw_id < 0x01) || (raw_id > 0x3f))
  1466. return ERR_PTR(-EOPNOTSUPP);
  1467. raw_event.cntr_mask = CNTR_ALL;
  1468. raw_event.event_id = raw_id;
  1469. return &raw_event;
  1470. }
  1471. static int __init
  1472. init_hw_perf_events(void)
  1473. {
  1474. int counters, irq;
  1475. int counter_bits;
  1476. pr_info("Performance counters: ");
  1477. counters = n_counters();
  1478. if (counters == 0) {
  1479. pr_cont("No available PMU.\n");
  1480. return -ENODEV;
  1481. }
  1482. #ifdef CONFIG_MIPS_PERF_SHARED_TC_COUNTERS
  1483. cpu_has_mipsmt_pertccounters = read_c0_config7() & (1<<19);
  1484. if (!cpu_has_mipsmt_pertccounters)
  1485. counters = counters_total_to_per_cpu(counters);
  1486. #endif
  1487. if (get_c0_perfcount_int)
  1488. irq = get_c0_perfcount_int();
  1489. else if (cp0_perfcount_irq >= 0)
  1490. irq = MIPS_CPU_IRQ_BASE + cp0_perfcount_irq;
  1491. else
  1492. irq = -1;
  1493. mipspmu.map_raw_event = mipsxx_pmu_map_raw_event;
  1494. switch (current_cpu_type()) {
  1495. case CPU_24K:
  1496. mipspmu.name = "mips/24K";
  1497. mipspmu.general_event_map = &mipsxxcore_event_map;
  1498. mipspmu.cache_event_map = &mipsxxcore_cache_map;
  1499. break;
  1500. case CPU_34K:
  1501. mipspmu.name = "mips/34K";
  1502. mipspmu.general_event_map = &mipsxxcore_event_map;
  1503. mipspmu.cache_event_map = &mipsxxcore_cache_map;
  1504. break;
  1505. case CPU_74K:
  1506. mipspmu.name = "mips/74K";
  1507. mipspmu.general_event_map = &mipsxxcore_event_map2;
  1508. mipspmu.cache_event_map = &mipsxxcore_cache_map2;
  1509. break;
  1510. case CPU_PROAPTIV:
  1511. mipspmu.name = "mips/proAptiv";
  1512. mipspmu.general_event_map = &mipsxxcore_event_map2;
  1513. mipspmu.cache_event_map = &mipsxxcore_cache_map2;
  1514. break;
  1515. case CPU_P5600:
  1516. mipspmu.name = "mips/P5600";
  1517. mipspmu.general_event_map = &mipsxxcore_event_map2;
  1518. mipspmu.cache_event_map = &mipsxxcore_cache_map2;
  1519. break;
  1520. case CPU_I6400:
  1521. mipspmu.name = "mips/I6400";
  1522. mipspmu.general_event_map = &mipsxxcore_event_map2;
  1523. mipspmu.cache_event_map = &mipsxxcore_cache_map2;
  1524. break;
  1525. case CPU_1004K:
  1526. mipspmu.name = "mips/1004K";
  1527. mipspmu.general_event_map = &mipsxxcore_event_map;
  1528. mipspmu.cache_event_map = &mipsxxcore_cache_map;
  1529. break;
  1530. case CPU_1074K:
  1531. mipspmu.name = "mips/1074K";
  1532. mipspmu.general_event_map = &mipsxxcore_event_map;
  1533. mipspmu.cache_event_map = &mipsxxcore_cache_map;
  1534. break;
  1535. case CPU_INTERAPTIV:
  1536. mipspmu.name = "mips/interAptiv";
  1537. mipspmu.general_event_map = &mipsxxcore_event_map;
  1538. mipspmu.cache_event_map = &mipsxxcore_cache_map;
  1539. break;
  1540. case CPU_LOONGSON1:
  1541. mipspmu.name = "mips/loongson1";
  1542. mipspmu.general_event_map = &mipsxxcore_event_map;
  1543. mipspmu.cache_event_map = &mipsxxcore_cache_map;
  1544. break;
  1545. case CPU_LOONGSON3:
  1546. mipspmu.name = "mips/loongson3";
  1547. mipspmu.general_event_map = &loongson3_event_map;
  1548. mipspmu.cache_event_map = &loongson3_cache_map;
  1549. break;
  1550. case CPU_CAVIUM_OCTEON:
  1551. case CPU_CAVIUM_OCTEON_PLUS:
  1552. case CPU_CAVIUM_OCTEON2:
  1553. mipspmu.name = "octeon";
  1554. mipspmu.general_event_map = &octeon_event_map;
  1555. mipspmu.cache_event_map = &octeon_cache_map;
  1556. mipspmu.map_raw_event = octeon_pmu_map_raw_event;
  1557. break;
  1558. case CPU_BMIPS5000:
  1559. mipspmu.name = "BMIPS5000";
  1560. mipspmu.general_event_map = &bmips5000_event_map;
  1561. mipspmu.cache_event_map = &bmips5000_cache_map;
  1562. break;
  1563. case CPU_XLP:
  1564. mipspmu.name = "xlp";
  1565. mipspmu.general_event_map = &xlp_event_map;
  1566. mipspmu.cache_event_map = &xlp_cache_map;
  1567. mipspmu.map_raw_event = xlp_pmu_map_raw_event;
  1568. break;
  1569. default:
  1570. pr_cont("Either hardware does not support performance "
  1571. "counters, or not yet implemented.\n");
  1572. return -ENODEV;
  1573. }
  1574. mipspmu.num_counters = counters;
  1575. mipspmu.irq = irq;
  1576. if (read_c0_perfctrl0() & M_PERFCTL_WIDE) {
  1577. mipspmu.max_period = (1ULL << 63) - 1;
  1578. mipspmu.valid_count = (1ULL << 63) - 1;
  1579. mipspmu.overflow = 1ULL << 63;
  1580. mipspmu.read_counter = mipsxx_pmu_read_counter_64;
  1581. mipspmu.write_counter = mipsxx_pmu_write_counter_64;
  1582. counter_bits = 64;
  1583. } else {
  1584. mipspmu.max_period = (1ULL << 31) - 1;
  1585. mipspmu.valid_count = (1ULL << 31) - 1;
  1586. mipspmu.overflow = 1ULL << 31;
  1587. mipspmu.read_counter = mipsxx_pmu_read_counter;
  1588. mipspmu.write_counter = mipsxx_pmu_write_counter;
  1589. counter_bits = 32;
  1590. }
  1591. on_each_cpu(reset_counters, (void *)(long)counters, 1);
  1592. pr_cont("%s PMU enabled, %d %d-bit counters available to each "
  1593. "CPU, irq %d%s\n", mipspmu.name, counters, counter_bits, irq,
  1594. irq < 0 ? " (share with timer interrupt)" : "");
  1595. perf_pmu_register(&pmu, "cpu", PERF_TYPE_RAW);
  1596. return 0;
  1597. }
  1598. early_initcall(init_hw_perf_events);