perf_event_mipsxx.c 27 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052
  1. #if defined(CONFIG_CPU_MIPS32) || defined(CONFIG_CPU_MIPS64) || \
  2. defined(CONFIG_CPU_R10000) || defined(CONFIG_CPU_SB1)
  3. #define M_CONFIG1_PC (1 << 4)
  4. #define M_PERFCTL_EXL (1UL << 0)
  5. #define M_PERFCTL_KERNEL (1UL << 1)
  6. #define M_PERFCTL_SUPERVISOR (1UL << 2)
  7. #define M_PERFCTL_USER (1UL << 3)
  8. #define M_PERFCTL_INTERRUPT_ENABLE (1UL << 4)
  9. #define M_PERFCTL_EVENT(event) (((event) & 0x3ff) << 5)
  10. #define M_PERFCTL_VPEID(vpe) ((vpe) << 16)
  11. #define M_PERFCTL_MT_EN(filter) ((filter) << 20)
  12. #define M_TC_EN_ALL M_PERFCTL_MT_EN(0)
  13. #define M_TC_EN_VPE M_PERFCTL_MT_EN(1)
  14. #define M_TC_EN_TC M_PERFCTL_MT_EN(2)
  15. #define M_PERFCTL_TCID(tcid) ((tcid) << 22)
  16. #define M_PERFCTL_WIDE (1UL << 30)
  17. #define M_PERFCTL_MORE (1UL << 31)
  18. #define M_PERFCTL_COUNT_EVENT_WHENEVER (M_PERFCTL_EXL | \
  19. M_PERFCTL_KERNEL | \
  20. M_PERFCTL_USER | \
  21. M_PERFCTL_SUPERVISOR | \
  22. M_PERFCTL_INTERRUPT_ENABLE)
  23. #ifdef CONFIG_MIPS_MT_SMP
  24. #define M_PERFCTL_CONFIG_MASK 0x3fff801f
  25. #else
  26. #define M_PERFCTL_CONFIG_MASK 0x1f
  27. #endif
  28. #define M_PERFCTL_EVENT_MASK 0xfe0
  29. #define M_COUNTER_OVERFLOW (1UL << 31)
  30. #ifdef CONFIG_MIPS_MT_SMP
  31. static int cpu_has_mipsmt_pertccounters;
  32. /*
  33. * FIXME: For VSMP, vpe_id() is redefined for Perf-events, because
  34. * cpu_data[cpuid].vpe_id reports 0 for _both_ CPUs.
  35. */
  36. #if defined(CONFIG_HW_PERF_EVENTS)
  37. #define vpe_id() (cpu_has_mipsmt_pertccounters ? \
  38. 0 : smp_processor_id())
  39. #else
  40. #define vpe_id() (cpu_has_mipsmt_pertccounters ? \
  41. 0 : cpu_data[smp_processor_id()].vpe_id)
  42. #endif
  43. /* Copied from op_model_mipsxx.c */
  44. static inline unsigned int vpe_shift(void)
  45. {
  46. if (num_possible_cpus() > 1)
  47. return 1;
  48. return 0;
  49. }
  50. #else /* !CONFIG_MIPS_MT_SMP */
  51. #define vpe_id() 0
  52. static inline unsigned int vpe_shift(void)
  53. {
  54. return 0;
  55. }
  56. #endif /* CONFIG_MIPS_MT_SMP */
  57. static inline unsigned int
  58. counters_total_to_per_cpu(unsigned int counters)
  59. {
  60. return counters >> vpe_shift();
  61. }
  62. static inline unsigned int
  63. counters_per_cpu_to_total(unsigned int counters)
  64. {
  65. return counters << vpe_shift();
  66. }
  67. #define __define_perf_accessors(r, n, np) \
  68. \
  69. static inline unsigned int r_c0_ ## r ## n(void) \
  70. { \
  71. unsigned int cpu = vpe_id(); \
  72. \
  73. switch (cpu) { \
  74. case 0: \
  75. return read_c0_ ## r ## n(); \
  76. case 1: \
  77. return read_c0_ ## r ## np(); \
  78. default: \
  79. BUG(); \
  80. } \
  81. return 0; \
  82. } \
  83. \
  84. static inline void w_c0_ ## r ## n(unsigned int value) \
  85. { \
  86. unsigned int cpu = vpe_id(); \
  87. \
  88. switch (cpu) { \
  89. case 0: \
  90. write_c0_ ## r ## n(value); \
  91. return; \
  92. case 1: \
  93. write_c0_ ## r ## np(value); \
  94. return; \
  95. default: \
  96. BUG(); \
  97. } \
  98. return; \
  99. } \
  100. __define_perf_accessors(perfcntr, 0, 2)
  101. __define_perf_accessors(perfcntr, 1, 3)
  102. __define_perf_accessors(perfcntr, 2, 0)
  103. __define_perf_accessors(perfcntr, 3, 1)
  104. __define_perf_accessors(perfctrl, 0, 2)
  105. __define_perf_accessors(perfctrl, 1, 3)
  106. __define_perf_accessors(perfctrl, 2, 0)
  107. __define_perf_accessors(perfctrl, 3, 1)
  108. static inline int __n_counters(void)
  109. {
  110. if (!(read_c0_config1() & M_CONFIG1_PC))
  111. return 0;
  112. if (!(read_c0_perfctrl0() & M_PERFCTL_MORE))
  113. return 1;
  114. if (!(read_c0_perfctrl1() & M_PERFCTL_MORE))
  115. return 2;
  116. if (!(read_c0_perfctrl2() & M_PERFCTL_MORE))
  117. return 3;
  118. return 4;
  119. }
  120. static inline int n_counters(void)
  121. {
  122. int counters;
  123. switch (current_cpu_type()) {
  124. case CPU_R10000:
  125. counters = 2;
  126. break;
  127. case CPU_R12000:
  128. case CPU_R14000:
  129. counters = 4;
  130. break;
  131. default:
  132. counters = __n_counters();
  133. }
  134. return counters;
  135. }
  136. static void reset_counters(void *arg)
  137. {
  138. int counters = (int)(long)arg;
  139. switch (counters) {
  140. case 4:
  141. w_c0_perfctrl3(0);
  142. w_c0_perfcntr3(0);
  143. case 3:
  144. w_c0_perfctrl2(0);
  145. w_c0_perfcntr2(0);
  146. case 2:
  147. w_c0_perfctrl1(0);
  148. w_c0_perfcntr1(0);
  149. case 1:
  150. w_c0_perfctrl0(0);
  151. w_c0_perfcntr0(0);
  152. }
  153. }
  154. static inline u64
  155. mipsxx_pmu_read_counter(unsigned int idx)
  156. {
  157. switch (idx) {
  158. case 0:
  159. return r_c0_perfcntr0();
  160. case 1:
  161. return r_c0_perfcntr1();
  162. case 2:
  163. return r_c0_perfcntr2();
  164. case 3:
  165. return r_c0_perfcntr3();
  166. default:
  167. WARN_ONCE(1, "Invalid performance counter number (%d)\n", idx);
  168. return 0;
  169. }
  170. }
  171. static inline void
  172. mipsxx_pmu_write_counter(unsigned int idx, u64 val)
  173. {
  174. switch (idx) {
  175. case 0:
  176. w_c0_perfcntr0(val);
  177. return;
  178. case 1:
  179. w_c0_perfcntr1(val);
  180. return;
  181. case 2:
  182. w_c0_perfcntr2(val);
  183. return;
  184. case 3:
  185. w_c0_perfcntr3(val);
  186. return;
  187. }
  188. }
  189. static inline unsigned int
  190. mipsxx_pmu_read_control(unsigned int idx)
  191. {
  192. switch (idx) {
  193. case 0:
  194. return r_c0_perfctrl0();
  195. case 1:
  196. return r_c0_perfctrl1();
  197. case 2:
  198. return r_c0_perfctrl2();
  199. case 3:
  200. return r_c0_perfctrl3();
  201. default:
  202. WARN_ONCE(1, "Invalid performance counter number (%d)\n", idx);
  203. return 0;
  204. }
  205. }
  206. static inline void
  207. mipsxx_pmu_write_control(unsigned int idx, unsigned int val)
  208. {
  209. switch (idx) {
  210. case 0:
  211. w_c0_perfctrl0(val);
  212. return;
  213. case 1:
  214. w_c0_perfctrl1(val);
  215. return;
  216. case 2:
  217. w_c0_perfctrl2(val);
  218. return;
  219. case 3:
  220. w_c0_perfctrl3(val);
  221. return;
  222. }
  223. }
  224. #ifdef CONFIG_MIPS_MT_SMP
  225. static DEFINE_RWLOCK(pmuint_rwlock);
  226. #endif
  227. /* 24K/34K/1004K cores can share the same event map. */
  228. static const struct mips_perf_event mipsxxcore_event_map
  229. [PERF_COUNT_HW_MAX] = {
  230. [PERF_COUNT_HW_CPU_CYCLES] = { 0x00, CNTR_EVEN | CNTR_ODD, P },
  231. [PERF_COUNT_HW_INSTRUCTIONS] = { 0x01, CNTR_EVEN | CNTR_ODD, T },
  232. [PERF_COUNT_HW_CACHE_REFERENCES] = { UNSUPPORTED_PERF_EVENT_ID },
  233. [PERF_COUNT_HW_CACHE_MISSES] = { UNSUPPORTED_PERF_EVENT_ID },
  234. [PERF_COUNT_HW_BRANCH_INSTRUCTIONS] = { 0x02, CNTR_EVEN, T },
  235. [PERF_COUNT_HW_BRANCH_MISSES] = { 0x02, CNTR_ODD, T },
  236. [PERF_COUNT_HW_BUS_CYCLES] = { UNSUPPORTED_PERF_EVENT_ID },
  237. };
  238. /* 74K core has different branch event code. */
  239. static const struct mips_perf_event mipsxx74Kcore_event_map
  240. [PERF_COUNT_HW_MAX] = {
  241. [PERF_COUNT_HW_CPU_CYCLES] = { 0x00, CNTR_EVEN | CNTR_ODD, P },
  242. [PERF_COUNT_HW_INSTRUCTIONS] = { 0x01, CNTR_EVEN | CNTR_ODD, T },
  243. [PERF_COUNT_HW_CACHE_REFERENCES] = { UNSUPPORTED_PERF_EVENT_ID },
  244. [PERF_COUNT_HW_CACHE_MISSES] = { UNSUPPORTED_PERF_EVENT_ID },
  245. [PERF_COUNT_HW_BRANCH_INSTRUCTIONS] = { 0x27, CNTR_EVEN, T },
  246. [PERF_COUNT_HW_BRANCH_MISSES] = { 0x27, CNTR_ODD, T },
  247. [PERF_COUNT_HW_BUS_CYCLES] = { UNSUPPORTED_PERF_EVENT_ID },
  248. };
  249. /* 24K/34K/1004K cores can share the same cache event map. */
  250. static const struct mips_perf_event mipsxxcore_cache_map
  251. [PERF_COUNT_HW_CACHE_MAX]
  252. [PERF_COUNT_HW_CACHE_OP_MAX]
  253. [PERF_COUNT_HW_CACHE_RESULT_MAX] = {
  254. [C(L1D)] = {
  255. /*
  256. * Like some other architectures (e.g. ARM), the performance
  257. * counters don't differentiate between read and write
  258. * accesses/misses, so this isn't strictly correct, but it's the
  259. * best we can do. Writes and reads get combined.
  260. */
  261. [C(OP_READ)] = {
  262. [C(RESULT_ACCESS)] = { 0x0a, CNTR_EVEN, T },
  263. [C(RESULT_MISS)] = { 0x0b, CNTR_EVEN | CNTR_ODD, T },
  264. },
  265. [C(OP_WRITE)] = {
  266. [C(RESULT_ACCESS)] = { 0x0a, CNTR_EVEN, T },
  267. [C(RESULT_MISS)] = { 0x0b, CNTR_EVEN | CNTR_ODD, T },
  268. },
  269. [C(OP_PREFETCH)] = {
  270. [C(RESULT_ACCESS)] = { UNSUPPORTED_PERF_EVENT_ID },
  271. [C(RESULT_MISS)] = { UNSUPPORTED_PERF_EVENT_ID },
  272. },
  273. },
  274. [C(L1I)] = {
  275. [C(OP_READ)] = {
  276. [C(RESULT_ACCESS)] = { 0x09, CNTR_EVEN, T },
  277. [C(RESULT_MISS)] = { 0x09, CNTR_ODD, T },
  278. },
  279. [C(OP_WRITE)] = {
  280. [C(RESULT_ACCESS)] = { 0x09, CNTR_EVEN, T },
  281. [C(RESULT_MISS)] = { 0x09, CNTR_ODD, T },
  282. },
  283. [C(OP_PREFETCH)] = {
  284. [C(RESULT_ACCESS)] = { 0x14, CNTR_EVEN, T },
  285. /*
  286. * Note that MIPS has only "hit" events countable for
  287. * the prefetch operation.
  288. */
  289. [C(RESULT_MISS)] = { UNSUPPORTED_PERF_EVENT_ID },
  290. },
  291. },
  292. [C(LL)] = {
  293. [C(OP_READ)] = {
  294. [C(RESULT_ACCESS)] = { 0x15, CNTR_ODD, P },
  295. [C(RESULT_MISS)] = { 0x16, CNTR_EVEN, P },
  296. },
  297. [C(OP_WRITE)] = {
  298. [C(RESULT_ACCESS)] = { 0x15, CNTR_ODD, P },
  299. [C(RESULT_MISS)] = { 0x16, CNTR_EVEN, P },
  300. },
  301. [C(OP_PREFETCH)] = {
  302. [C(RESULT_ACCESS)] = { UNSUPPORTED_PERF_EVENT_ID },
  303. [C(RESULT_MISS)] = { UNSUPPORTED_PERF_EVENT_ID },
  304. },
  305. },
  306. [C(DTLB)] = {
  307. [C(OP_READ)] = {
  308. [C(RESULT_ACCESS)] = { 0x06, CNTR_EVEN, T },
  309. [C(RESULT_MISS)] = { 0x06, CNTR_ODD, T },
  310. },
  311. [C(OP_WRITE)] = {
  312. [C(RESULT_ACCESS)] = { 0x06, CNTR_EVEN, T },
  313. [C(RESULT_MISS)] = { 0x06, CNTR_ODD, T },
  314. },
  315. [C(OP_PREFETCH)] = {
  316. [C(RESULT_ACCESS)] = { UNSUPPORTED_PERF_EVENT_ID },
  317. [C(RESULT_MISS)] = { UNSUPPORTED_PERF_EVENT_ID },
  318. },
  319. },
  320. [C(ITLB)] = {
  321. [C(OP_READ)] = {
  322. [C(RESULT_ACCESS)] = { 0x05, CNTR_EVEN, T },
  323. [C(RESULT_MISS)] = { 0x05, CNTR_ODD, T },
  324. },
  325. [C(OP_WRITE)] = {
  326. [C(RESULT_ACCESS)] = { 0x05, CNTR_EVEN, T },
  327. [C(RESULT_MISS)] = { 0x05, CNTR_ODD, T },
  328. },
  329. [C(OP_PREFETCH)] = {
  330. [C(RESULT_ACCESS)] = { UNSUPPORTED_PERF_EVENT_ID },
  331. [C(RESULT_MISS)] = { UNSUPPORTED_PERF_EVENT_ID },
  332. },
  333. },
  334. [C(BPU)] = {
  335. /* Using the same code for *HW_BRANCH* */
  336. [C(OP_READ)] = {
  337. [C(RESULT_ACCESS)] = { 0x02, CNTR_EVEN, T },
  338. [C(RESULT_MISS)] = { 0x02, CNTR_ODD, T },
  339. },
  340. [C(OP_WRITE)] = {
  341. [C(RESULT_ACCESS)] = { 0x02, CNTR_EVEN, T },
  342. [C(RESULT_MISS)] = { 0x02, CNTR_ODD, T },
  343. },
  344. [C(OP_PREFETCH)] = {
  345. [C(RESULT_ACCESS)] = { UNSUPPORTED_PERF_EVENT_ID },
  346. [C(RESULT_MISS)] = { UNSUPPORTED_PERF_EVENT_ID },
  347. },
  348. },
  349. };
  350. /* 74K core has completely different cache event map. */
  351. static const struct mips_perf_event mipsxx74Kcore_cache_map
  352. [PERF_COUNT_HW_CACHE_MAX]
  353. [PERF_COUNT_HW_CACHE_OP_MAX]
  354. [PERF_COUNT_HW_CACHE_RESULT_MAX] = {
  355. [C(L1D)] = {
  356. /*
  357. * Like some other architectures (e.g. ARM), the performance
  358. * counters don't differentiate between read and write
  359. * accesses/misses, so this isn't strictly correct, but it's the
  360. * best we can do. Writes and reads get combined.
  361. */
  362. [C(OP_READ)] = {
  363. [C(RESULT_ACCESS)] = { 0x17, CNTR_ODD, T },
  364. [C(RESULT_MISS)] = { 0x18, CNTR_ODD, T },
  365. },
  366. [C(OP_WRITE)] = {
  367. [C(RESULT_ACCESS)] = { 0x17, CNTR_ODD, T },
  368. [C(RESULT_MISS)] = { 0x18, CNTR_ODD, T },
  369. },
  370. [C(OP_PREFETCH)] = {
  371. [C(RESULT_ACCESS)] = { UNSUPPORTED_PERF_EVENT_ID },
  372. [C(RESULT_MISS)] = { UNSUPPORTED_PERF_EVENT_ID },
  373. },
  374. },
  375. [C(L1I)] = {
  376. [C(OP_READ)] = {
  377. [C(RESULT_ACCESS)] = { 0x06, CNTR_EVEN, T },
  378. [C(RESULT_MISS)] = { 0x06, CNTR_ODD, T },
  379. },
  380. [C(OP_WRITE)] = {
  381. [C(RESULT_ACCESS)] = { 0x06, CNTR_EVEN, T },
  382. [C(RESULT_MISS)] = { 0x06, CNTR_ODD, T },
  383. },
  384. [C(OP_PREFETCH)] = {
  385. [C(RESULT_ACCESS)] = { 0x34, CNTR_EVEN, T },
  386. /*
  387. * Note that MIPS has only "hit" events countable for
  388. * the prefetch operation.
  389. */
  390. [C(RESULT_MISS)] = { UNSUPPORTED_PERF_EVENT_ID },
  391. },
  392. },
  393. [C(LL)] = {
  394. [C(OP_READ)] = {
  395. [C(RESULT_ACCESS)] = { 0x1c, CNTR_ODD, P },
  396. [C(RESULT_MISS)] = { 0x1d, CNTR_EVEN | CNTR_ODD, P },
  397. },
  398. [C(OP_WRITE)] = {
  399. [C(RESULT_ACCESS)] = { 0x1c, CNTR_ODD, P },
  400. [C(RESULT_MISS)] = { 0x1d, CNTR_EVEN | CNTR_ODD, P },
  401. },
  402. [C(OP_PREFETCH)] = {
  403. [C(RESULT_ACCESS)] = { UNSUPPORTED_PERF_EVENT_ID },
  404. [C(RESULT_MISS)] = { UNSUPPORTED_PERF_EVENT_ID },
  405. },
  406. },
  407. [C(DTLB)] = {
  408. /* 74K core does not have specific DTLB events. */
  409. [C(OP_READ)] = {
  410. [C(RESULT_ACCESS)] = { UNSUPPORTED_PERF_EVENT_ID },
  411. [C(RESULT_MISS)] = { UNSUPPORTED_PERF_EVENT_ID },
  412. },
  413. [C(OP_WRITE)] = {
  414. [C(RESULT_ACCESS)] = { UNSUPPORTED_PERF_EVENT_ID },
  415. [C(RESULT_MISS)] = { UNSUPPORTED_PERF_EVENT_ID },
  416. },
  417. [C(OP_PREFETCH)] = {
  418. [C(RESULT_ACCESS)] = { UNSUPPORTED_PERF_EVENT_ID },
  419. [C(RESULT_MISS)] = { UNSUPPORTED_PERF_EVENT_ID },
  420. },
  421. },
  422. [C(ITLB)] = {
  423. [C(OP_READ)] = {
  424. [C(RESULT_ACCESS)] = { 0x04, CNTR_EVEN, T },
  425. [C(RESULT_MISS)] = { 0x04, CNTR_ODD, T },
  426. },
  427. [C(OP_WRITE)] = {
  428. [C(RESULT_ACCESS)] = { 0x04, CNTR_EVEN, T },
  429. [C(RESULT_MISS)] = { 0x04, CNTR_ODD, T },
  430. },
  431. [C(OP_PREFETCH)] = {
  432. [C(RESULT_ACCESS)] = { UNSUPPORTED_PERF_EVENT_ID },
  433. [C(RESULT_MISS)] = { UNSUPPORTED_PERF_EVENT_ID },
  434. },
  435. },
  436. [C(BPU)] = {
  437. /* Using the same code for *HW_BRANCH* */
  438. [C(OP_READ)] = {
  439. [C(RESULT_ACCESS)] = { 0x27, CNTR_EVEN, T },
  440. [C(RESULT_MISS)] = { 0x27, CNTR_ODD, T },
  441. },
  442. [C(OP_WRITE)] = {
  443. [C(RESULT_ACCESS)] = { 0x27, CNTR_EVEN, T },
  444. [C(RESULT_MISS)] = { 0x27, CNTR_ODD, T },
  445. },
  446. [C(OP_PREFETCH)] = {
  447. [C(RESULT_ACCESS)] = { UNSUPPORTED_PERF_EVENT_ID },
  448. [C(RESULT_MISS)] = { UNSUPPORTED_PERF_EVENT_ID },
  449. },
  450. },
  451. };
  452. #ifdef CONFIG_MIPS_MT_SMP
  453. static void
  454. check_and_calc_range(struct perf_event *event,
  455. const struct mips_perf_event *pev)
  456. {
  457. struct hw_perf_event *hwc = &event->hw;
  458. if (event->cpu >= 0) {
  459. if (pev->range > V) {
  460. /*
  461. * The user selected an event that is processor
  462. * wide, while expecting it to be VPE wide.
  463. */
  464. hwc->config_base |= M_TC_EN_ALL;
  465. } else {
  466. /*
  467. * FIXME: cpu_data[event->cpu].vpe_id reports 0
  468. * for both CPUs.
  469. */
  470. hwc->config_base |= M_PERFCTL_VPEID(event->cpu);
  471. hwc->config_base |= M_TC_EN_VPE;
  472. }
  473. } else
  474. hwc->config_base |= M_TC_EN_ALL;
  475. }
  476. #else
  477. static void
  478. check_and_calc_range(struct perf_event *event,
  479. const struct mips_perf_event *pev)
  480. {
  481. }
  482. #endif
  483. static int __hw_perf_event_init(struct perf_event *event)
  484. {
  485. struct perf_event_attr *attr = &event->attr;
  486. struct hw_perf_event *hwc = &event->hw;
  487. const struct mips_perf_event *pev;
  488. int err;
  489. /* Returning MIPS event descriptor for generic perf event. */
  490. if (PERF_TYPE_HARDWARE == event->attr.type) {
  491. if (event->attr.config >= PERF_COUNT_HW_MAX)
  492. return -EINVAL;
  493. pev = mipspmu_map_general_event(event->attr.config);
  494. } else if (PERF_TYPE_HW_CACHE == event->attr.type) {
  495. pev = mipspmu_map_cache_event(event->attr.config);
  496. } else if (PERF_TYPE_RAW == event->attr.type) {
  497. /* We are working on the global raw event. */
  498. mutex_lock(&raw_event_mutex);
  499. pev = mipspmu->map_raw_event(event->attr.config);
  500. } else {
  501. /* The event type is not (yet) supported. */
  502. return -EOPNOTSUPP;
  503. }
  504. if (IS_ERR(pev)) {
  505. if (PERF_TYPE_RAW == event->attr.type)
  506. mutex_unlock(&raw_event_mutex);
  507. return PTR_ERR(pev);
  508. }
  509. /*
  510. * We allow max flexibility on how each individual counter shared
  511. * by the single CPU operates (the mode exclusion and the range).
  512. */
  513. hwc->config_base = M_PERFCTL_INTERRUPT_ENABLE;
  514. /* Calculate range bits and validate it. */
  515. if (num_possible_cpus() > 1)
  516. check_and_calc_range(event, pev);
  517. hwc->event_base = mipspmu_perf_event_encode(pev);
  518. if (PERF_TYPE_RAW == event->attr.type)
  519. mutex_unlock(&raw_event_mutex);
  520. if (!attr->exclude_user)
  521. hwc->config_base |= M_PERFCTL_USER;
  522. if (!attr->exclude_kernel) {
  523. hwc->config_base |= M_PERFCTL_KERNEL;
  524. /* MIPS kernel mode: KSU == 00b || EXL == 1 || ERL == 1 */
  525. hwc->config_base |= M_PERFCTL_EXL;
  526. }
  527. if (!attr->exclude_hv)
  528. hwc->config_base |= M_PERFCTL_SUPERVISOR;
  529. hwc->config_base &= M_PERFCTL_CONFIG_MASK;
  530. /*
  531. * The event can belong to another cpu. We do not assign a local
  532. * counter for it for now.
  533. */
  534. hwc->idx = -1;
  535. hwc->config = 0;
  536. if (!hwc->sample_period) {
  537. hwc->sample_period = MAX_PERIOD;
  538. hwc->last_period = hwc->sample_period;
  539. local64_set(&hwc->period_left, hwc->sample_period);
  540. }
  541. err = 0;
  542. if (event->group_leader != event) {
  543. err = validate_group(event);
  544. if (err)
  545. return -EINVAL;
  546. }
  547. event->destroy = hw_perf_event_destroy;
  548. return err;
  549. }
  550. static void pause_local_counters(void)
  551. {
  552. struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
  553. int counters = mipspmu->num_counters;
  554. unsigned long flags;
  555. local_irq_save(flags);
  556. switch (counters) {
  557. case 4:
  558. cpuc->saved_ctrl[3] = r_c0_perfctrl3();
  559. w_c0_perfctrl3(cpuc->saved_ctrl[3] &
  560. ~M_PERFCTL_COUNT_EVENT_WHENEVER);
  561. case 3:
  562. cpuc->saved_ctrl[2] = r_c0_perfctrl2();
  563. w_c0_perfctrl2(cpuc->saved_ctrl[2] &
  564. ~M_PERFCTL_COUNT_EVENT_WHENEVER);
  565. case 2:
  566. cpuc->saved_ctrl[1] = r_c0_perfctrl1();
  567. w_c0_perfctrl1(cpuc->saved_ctrl[1] &
  568. ~M_PERFCTL_COUNT_EVENT_WHENEVER);
  569. case 1:
  570. cpuc->saved_ctrl[0] = r_c0_perfctrl0();
  571. w_c0_perfctrl0(cpuc->saved_ctrl[0] &
  572. ~M_PERFCTL_COUNT_EVENT_WHENEVER);
  573. }
  574. local_irq_restore(flags);
  575. }
  576. static void resume_local_counters(void)
  577. {
  578. struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
  579. int counters = mipspmu->num_counters;
  580. unsigned long flags;
  581. local_irq_save(flags);
  582. switch (counters) {
  583. case 4:
  584. w_c0_perfctrl3(cpuc->saved_ctrl[3]);
  585. case 3:
  586. w_c0_perfctrl2(cpuc->saved_ctrl[2]);
  587. case 2:
  588. w_c0_perfctrl1(cpuc->saved_ctrl[1]);
  589. case 1:
  590. w_c0_perfctrl0(cpuc->saved_ctrl[0]);
  591. }
  592. local_irq_restore(flags);
  593. }
  594. static int mipsxx_pmu_handle_shared_irq(void)
  595. {
  596. struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
  597. struct perf_sample_data data;
  598. unsigned int counters = mipspmu->num_counters;
  599. unsigned int counter;
  600. int handled = IRQ_NONE;
  601. struct pt_regs *regs;
  602. if (cpu_has_mips_r2 && !(read_c0_cause() & (1 << 26)))
  603. return handled;
  604. /*
  605. * First we pause the local counters, so that when we are locked
  606. * here, the counters are all paused. When it gets locked due to
  607. * perf_disable(), the timer interrupt handler will be delayed.
  608. *
  609. * See also mipsxx_pmu_start().
  610. */
  611. pause_local_counters();
  612. #ifdef CONFIG_MIPS_MT_SMP
  613. read_lock(&pmuint_rwlock);
  614. #endif
  615. regs = get_irq_regs();
  616. perf_sample_data_init(&data, 0);
  617. switch (counters) {
  618. #define HANDLE_COUNTER(n) \
  619. case n + 1: \
  620. if (test_bit(n, cpuc->used_mask)) { \
  621. counter = r_c0_perfcntr ## n(); \
  622. if (counter & M_COUNTER_OVERFLOW) { \
  623. w_c0_perfcntr ## n(counter & \
  624. VALID_COUNT); \
  625. if (test_and_change_bit(n, cpuc->msbs)) \
  626. handle_associated_event(cpuc, \
  627. n, &data, regs); \
  628. handled = IRQ_HANDLED; \
  629. } \
  630. }
  631. HANDLE_COUNTER(3)
  632. HANDLE_COUNTER(2)
  633. HANDLE_COUNTER(1)
  634. HANDLE_COUNTER(0)
  635. }
  636. /*
  637. * Do all the work for the pending perf events. We can do this
  638. * in here because the performance counter interrupt is a regular
  639. * interrupt, not NMI.
  640. */
  641. if (handled == IRQ_HANDLED)
  642. perf_event_do_pending();
  643. #ifdef CONFIG_MIPS_MT_SMP
  644. read_unlock(&pmuint_rwlock);
  645. #endif
  646. resume_local_counters();
  647. return handled;
  648. }
  649. static irqreturn_t
  650. mipsxx_pmu_handle_irq(int irq, void *dev)
  651. {
  652. return mipsxx_pmu_handle_shared_irq();
  653. }
  654. static void mipsxx_pmu_start(void)
  655. {
  656. #ifdef CONFIG_MIPS_MT_SMP
  657. write_unlock(&pmuint_rwlock);
  658. #endif
  659. resume_local_counters();
  660. }
  661. /*
  662. * MIPS performance counters can be per-TC. The control registers can
  663. * not be directly accessed accross CPUs. Hence if we want to do global
  664. * control, we need cross CPU calls. on_each_cpu() can help us, but we
  665. * can not make sure this function is called with interrupts enabled. So
  666. * here we pause local counters and then grab a rwlock and leave the
  667. * counters on other CPUs alone. If any counter interrupt raises while
  668. * we own the write lock, simply pause local counters on that CPU and
  669. * spin in the handler. Also we know we won't be switched to another
  670. * CPU after pausing local counters and before grabbing the lock.
  671. */
  672. static void mipsxx_pmu_stop(void)
  673. {
  674. pause_local_counters();
  675. #ifdef CONFIG_MIPS_MT_SMP
  676. write_lock(&pmuint_rwlock);
  677. #endif
  678. }
  679. static int
  680. mipsxx_pmu_alloc_counter(struct cpu_hw_events *cpuc,
  681. struct hw_perf_event *hwc)
  682. {
  683. int i;
  684. /*
  685. * We only need to care the counter mask. The range has been
  686. * checked definitely.
  687. */
  688. unsigned long cntr_mask = (hwc->event_base >> 8) & 0xffff;
  689. for (i = mipspmu->num_counters - 1; i >= 0; i--) {
  690. /*
  691. * Note that some MIPS perf events can be counted by both
  692. * even and odd counters, wheresas many other are only by
  693. * even _or_ odd counters. This introduces an issue that
  694. * when the former kind of event takes the counter the
  695. * latter kind of event wants to use, then the "counter
  696. * allocation" for the latter event will fail. In fact if
  697. * they can be dynamically swapped, they both feel happy.
  698. * But here we leave this issue alone for now.
  699. */
  700. if (test_bit(i, &cntr_mask) &&
  701. !test_and_set_bit(i, cpuc->used_mask))
  702. return i;
  703. }
  704. return -EAGAIN;
  705. }
  706. static void
  707. mipsxx_pmu_enable_event(struct hw_perf_event *evt, int idx)
  708. {
  709. struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
  710. unsigned long flags;
  711. WARN_ON(idx < 0 || idx >= mipspmu->num_counters);
  712. local_irq_save(flags);
  713. cpuc->saved_ctrl[idx] = M_PERFCTL_EVENT(evt->event_base & 0xff) |
  714. (evt->config_base & M_PERFCTL_CONFIG_MASK) |
  715. /* Make sure interrupt enabled. */
  716. M_PERFCTL_INTERRUPT_ENABLE;
  717. /*
  718. * We do not actually let the counter run. Leave it until start().
  719. */
  720. local_irq_restore(flags);
  721. }
  722. static void
  723. mipsxx_pmu_disable_event(int idx)
  724. {
  725. struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
  726. unsigned long flags;
  727. WARN_ON(idx < 0 || idx >= mipspmu->num_counters);
  728. local_irq_save(flags);
  729. cpuc->saved_ctrl[idx] = mipsxx_pmu_read_control(idx) &
  730. ~M_PERFCTL_COUNT_EVENT_WHENEVER;
  731. mipsxx_pmu_write_control(idx, cpuc->saved_ctrl[idx]);
  732. local_irq_restore(flags);
  733. }
  734. /* 24K */
  735. #define IS_UNSUPPORTED_24K_EVENT(r, b) \
  736. ((b) == 12 || (r) == 151 || (r) == 152 || (b) == 26 || \
  737. (b) == 27 || (r) == 28 || (r) == 158 || (b) == 31 || \
  738. (b) == 32 || (b) == 34 || (b) == 36 || (r) == 168 || \
  739. (r) == 172 || (b) == 47 || ((b) >= 56 && (b) <= 63) || \
  740. ((b) >= 68 && (b) <= 127))
  741. #define IS_BOTH_COUNTERS_24K_EVENT(b) \
  742. ((b) == 0 || (b) == 1 || (b) == 11)
  743. /* 34K */
  744. #define IS_UNSUPPORTED_34K_EVENT(r, b) \
  745. ((b) == 12 || (r) == 27 || (r) == 158 || (b) == 36 || \
  746. (b) == 38 || (r) == 175 || ((b) >= 56 && (b) <= 63) || \
  747. ((b) >= 68 && (b) <= 127))
  748. #define IS_BOTH_COUNTERS_34K_EVENT(b) \
  749. ((b) == 0 || (b) == 1 || (b) == 11)
  750. #ifdef CONFIG_MIPS_MT_SMP
  751. #define IS_RANGE_P_34K_EVENT(r, b) \
  752. ((b) == 0 || (r) == 18 || (b) == 21 || (b) == 22 || \
  753. (b) == 25 || (b) == 39 || (r) == 44 || (r) == 174 || \
  754. (r) == 176 || ((b) >= 50 && (b) <= 55) || \
  755. ((b) >= 64 && (b) <= 67))
  756. #define IS_RANGE_V_34K_EVENT(r) ((r) == 47)
  757. #endif
  758. /* 74K */
  759. #define IS_UNSUPPORTED_74K_EVENT(r, b) \
  760. ((r) == 5 || ((r) >= 135 && (r) <= 137) || \
  761. ((b) >= 10 && (b) <= 12) || (b) == 22 || (b) == 27 || \
  762. (b) == 33 || (b) == 34 || ((b) >= 47 && (b) <= 49) || \
  763. (r) == 178 || (b) == 55 || (b) == 57 || (b) == 60 || \
  764. (b) == 61 || (r) == 62 || (r) == 191 || \
  765. ((b) >= 64 && (b) <= 127))
  766. #define IS_BOTH_COUNTERS_74K_EVENT(b) \
  767. ((b) == 0 || (b) == 1)
  768. /* 1004K */
  769. #define IS_UNSUPPORTED_1004K_EVENT(r, b) \
  770. ((b) == 12 || (r) == 27 || (r) == 158 || (b) == 38 || \
  771. (r) == 175 || (b) == 63 || ((b) >= 68 && (b) <= 127))
  772. #define IS_BOTH_COUNTERS_1004K_EVENT(b) \
  773. ((b) == 0 || (b) == 1 || (b) == 11)
  774. #ifdef CONFIG_MIPS_MT_SMP
  775. #define IS_RANGE_P_1004K_EVENT(r, b) \
  776. ((b) == 0 || (r) == 18 || (b) == 21 || (b) == 22 || \
  777. (b) == 25 || (b) == 36 || (b) == 39 || (r) == 44 || \
  778. (r) == 174 || (r) == 176 || ((b) >= 50 && (b) <= 59) || \
  779. (r) == 188 || (b) == 61 || (b) == 62 || \
  780. ((b) >= 64 && (b) <= 67))
  781. #define IS_RANGE_V_1004K_EVENT(r) ((r) == 47)
  782. #endif
  783. /*
  784. * User can use 0-255 raw events, where 0-127 for the events of even
  785. * counters, and 128-255 for odd counters. Note that bit 7 is used to
  786. * indicate the parity. So, for example, when user wants to take the
  787. * Event Num of 15 for odd counters (by referring to the user manual),
  788. * then 128 needs to be added to 15 as the input for the event config,
  789. * i.e., 143 (0x8F) to be used.
  790. */
  791. static const struct mips_perf_event *
  792. mipsxx_pmu_map_raw_event(u64 config)
  793. {
  794. unsigned int raw_id = config & 0xff;
  795. unsigned int base_id = raw_id & 0x7f;
  796. switch (current_cpu_type()) {
  797. case CPU_24K:
  798. if (IS_UNSUPPORTED_24K_EVENT(raw_id, base_id))
  799. return ERR_PTR(-EOPNOTSUPP);
  800. raw_event.event_id = base_id;
  801. if (IS_BOTH_COUNTERS_24K_EVENT(base_id))
  802. raw_event.cntr_mask = CNTR_EVEN | CNTR_ODD;
  803. else
  804. raw_event.cntr_mask =
  805. raw_id > 127 ? CNTR_ODD : CNTR_EVEN;
  806. #ifdef CONFIG_MIPS_MT_SMP
  807. /*
  808. * This is actually doing nothing. Non-multithreading
  809. * CPUs will not check and calculate the range.
  810. */
  811. raw_event.range = P;
  812. #endif
  813. break;
  814. case CPU_34K:
  815. if (IS_UNSUPPORTED_34K_EVENT(raw_id, base_id))
  816. return ERR_PTR(-EOPNOTSUPP);
  817. raw_event.event_id = base_id;
  818. if (IS_BOTH_COUNTERS_34K_EVENT(base_id))
  819. raw_event.cntr_mask = CNTR_EVEN | CNTR_ODD;
  820. else
  821. raw_event.cntr_mask =
  822. raw_id > 127 ? CNTR_ODD : CNTR_EVEN;
  823. #ifdef CONFIG_MIPS_MT_SMP
  824. if (IS_RANGE_P_34K_EVENT(raw_id, base_id))
  825. raw_event.range = P;
  826. else if (unlikely(IS_RANGE_V_34K_EVENT(raw_id)))
  827. raw_event.range = V;
  828. else
  829. raw_event.range = T;
  830. #endif
  831. break;
  832. case CPU_74K:
  833. if (IS_UNSUPPORTED_74K_EVENT(raw_id, base_id))
  834. return ERR_PTR(-EOPNOTSUPP);
  835. raw_event.event_id = base_id;
  836. if (IS_BOTH_COUNTERS_74K_EVENT(base_id))
  837. raw_event.cntr_mask = CNTR_EVEN | CNTR_ODD;
  838. else
  839. raw_event.cntr_mask =
  840. raw_id > 127 ? CNTR_ODD : CNTR_EVEN;
  841. #ifdef CONFIG_MIPS_MT_SMP
  842. raw_event.range = P;
  843. #endif
  844. break;
  845. case CPU_1004K:
  846. if (IS_UNSUPPORTED_1004K_EVENT(raw_id, base_id))
  847. return ERR_PTR(-EOPNOTSUPP);
  848. raw_event.event_id = base_id;
  849. if (IS_BOTH_COUNTERS_1004K_EVENT(base_id))
  850. raw_event.cntr_mask = CNTR_EVEN | CNTR_ODD;
  851. else
  852. raw_event.cntr_mask =
  853. raw_id > 127 ? CNTR_ODD : CNTR_EVEN;
  854. #ifdef CONFIG_MIPS_MT_SMP
  855. if (IS_RANGE_P_1004K_EVENT(raw_id, base_id))
  856. raw_event.range = P;
  857. else if (unlikely(IS_RANGE_V_1004K_EVENT(raw_id)))
  858. raw_event.range = V;
  859. else
  860. raw_event.range = T;
  861. #endif
  862. break;
  863. }
  864. return &raw_event;
  865. }
  866. static struct mips_pmu mipsxxcore_pmu = {
  867. .handle_irq = mipsxx_pmu_handle_irq,
  868. .handle_shared_irq = mipsxx_pmu_handle_shared_irq,
  869. .start = mipsxx_pmu_start,
  870. .stop = mipsxx_pmu_stop,
  871. .alloc_counter = mipsxx_pmu_alloc_counter,
  872. .read_counter = mipsxx_pmu_read_counter,
  873. .write_counter = mipsxx_pmu_write_counter,
  874. .enable_event = mipsxx_pmu_enable_event,
  875. .disable_event = mipsxx_pmu_disable_event,
  876. .map_raw_event = mipsxx_pmu_map_raw_event,
  877. .general_event_map = &mipsxxcore_event_map,
  878. .cache_event_map = &mipsxxcore_cache_map,
  879. };
  880. static struct mips_pmu mipsxx74Kcore_pmu = {
  881. .handle_irq = mipsxx_pmu_handle_irq,
  882. .handle_shared_irq = mipsxx_pmu_handle_shared_irq,
  883. .start = mipsxx_pmu_start,
  884. .stop = mipsxx_pmu_stop,
  885. .alloc_counter = mipsxx_pmu_alloc_counter,
  886. .read_counter = mipsxx_pmu_read_counter,
  887. .write_counter = mipsxx_pmu_write_counter,
  888. .enable_event = mipsxx_pmu_enable_event,
  889. .disable_event = mipsxx_pmu_disable_event,
  890. .map_raw_event = mipsxx_pmu_map_raw_event,
  891. .general_event_map = &mipsxx74Kcore_event_map,
  892. .cache_event_map = &mipsxx74Kcore_cache_map,
  893. };
  894. static int __init
  895. init_hw_perf_events(void)
  896. {
  897. int counters, irq;
  898. pr_info("Performance counters: ");
  899. counters = n_counters();
  900. if (counters == 0) {
  901. pr_cont("No available PMU.\n");
  902. return -ENODEV;
  903. }
  904. #ifdef CONFIG_MIPS_MT_SMP
  905. cpu_has_mipsmt_pertccounters = read_c0_config7() & (1<<19);
  906. if (!cpu_has_mipsmt_pertccounters)
  907. counters = counters_total_to_per_cpu(counters);
  908. #endif
  909. #ifdef MSC01E_INT_BASE
  910. if (cpu_has_veic) {
  911. /*
  912. * Using platform specific interrupt controller defines.
  913. */
  914. irq = MSC01E_INT_BASE + MSC01E_INT_PERFCTR;
  915. } else {
  916. #endif
  917. if (cp0_perfcount_irq >= 0)
  918. irq = MIPS_CPU_IRQ_BASE + cp0_perfcount_irq;
  919. else
  920. irq = -1;
  921. #ifdef MSC01E_INT_BASE
  922. }
  923. #endif
  924. on_each_cpu(reset_counters, (void *)(long)counters, 1);
  925. switch (current_cpu_type()) {
  926. case CPU_24K:
  927. mipsxxcore_pmu.name = "mips/24K";
  928. mipsxxcore_pmu.num_counters = counters;
  929. mipsxxcore_pmu.irq = irq;
  930. mipspmu = &mipsxxcore_pmu;
  931. break;
  932. case CPU_34K:
  933. mipsxxcore_pmu.name = "mips/34K";
  934. mipsxxcore_pmu.num_counters = counters;
  935. mipsxxcore_pmu.irq = irq;
  936. mipspmu = &mipsxxcore_pmu;
  937. break;
  938. case CPU_74K:
  939. mipsxx74Kcore_pmu.name = "mips/74K";
  940. mipsxx74Kcore_pmu.num_counters = counters;
  941. mipsxx74Kcore_pmu.irq = irq;
  942. mipspmu = &mipsxx74Kcore_pmu;
  943. break;
  944. case CPU_1004K:
  945. mipsxxcore_pmu.name = "mips/1004K";
  946. mipsxxcore_pmu.num_counters = counters;
  947. mipsxxcore_pmu.irq = irq;
  948. mipspmu = &mipsxxcore_pmu;
  949. break;
  950. default:
  951. pr_cont("Either hardware does not support performance "
  952. "counters, or not yet implemented.\n");
  953. return -ENODEV;
  954. }
  955. if (mipspmu)
  956. pr_cont("%s PMU enabled, %d counters available to each "
  957. "CPU, irq %d%s\n", mipspmu->name, counters, irq,
  958. irq < 0 ? " (share with timer interrupt)" : "");
  959. return 0;
  960. }
  961. early_initcall(init_hw_perf_events);
  962. #endif /* defined(CONFIG_CPU_MIPS32)... */