perf_event.c 25 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028
  1. /* Performance event support for sparc64.
  2. *
  3. * Copyright (C) 2009 David S. Miller <davem@davemloft.net>
  4. *
  5. * This code is based almost entirely upon the x86 perf event
  6. * code, which is:
  7. *
  8. * Copyright (C) 2008 Thomas Gleixner <tglx@linutronix.de>
  9. * Copyright (C) 2008-2009 Red Hat, Inc., Ingo Molnar
  10. * Copyright (C) 2009 Jaswinder Singh Rajput
  11. * Copyright (C) 2009 Advanced Micro Devices, Inc., Robert Richter
  12. * Copyright (C) 2008-2009 Red Hat, Inc., Peter Zijlstra <pzijlstr@redhat.com>
  13. */
  14. #include <linux/perf_event.h>
  15. #include <linux/kprobes.h>
  16. #include <linux/kernel.h>
  17. #include <linux/kdebug.h>
  18. #include <linux/mutex.h>
  19. #include <asm/cpudata.h>
  20. #include <asm/atomic.h>
  21. #include <asm/nmi.h>
  22. #include <asm/pcr.h>
  23. /* Sparc64 chips have two performance counters, 32-bits each, with
  24. * overflow interrupts generated on transition from 0xffffffff to 0.
  25. * The counters are accessed in one go using a 64-bit register.
  26. *
  27. * Both counters are controlled using a single control register. The
  28. * only way to stop all sampling is to clear all of the context (user,
  29. * supervisor, hypervisor) sampling enable bits. But these bits apply
  30. * to both counters, thus the two counters can't be enabled/disabled
  31. * individually.
  32. *
  33. * The control register has two event fields, one for each of the two
  34. * counters. It's thus nearly impossible to have one counter going
  35. * while keeping the other one stopped. Therefore it is possible to
  36. * get overflow interrupts for counters not currently "in use" and
  37. * that condition must be checked in the overflow interrupt handler.
  38. *
  39. * So we use a hack, in that we program inactive counters with the
  40. * "sw_count0" and "sw_count1" events. These count how many times
  41. * the instruction "sethi %hi(0xfc000), %g0" is executed. It's an
  42. * unusual way to encode a NOP and therefore will not trigger in
  43. * normal code.
  44. */
  45. #define MAX_HWEVENTS 2
  46. #define MAX_PERIOD ((1UL << 32) - 1)
  47. #define PIC_UPPER_INDEX 0
  48. #define PIC_LOWER_INDEX 1
  49. struct cpu_hw_events {
  50. struct perf_event *events[MAX_HWEVENTS];
  51. unsigned long used_mask[BITS_TO_LONGS(MAX_HWEVENTS)];
  52. unsigned long active_mask[BITS_TO_LONGS(MAX_HWEVENTS)];
  53. int enabled;
  54. };
  55. DEFINE_PER_CPU(struct cpu_hw_events, cpu_hw_events) = { .enabled = 1, };
  56. struct perf_event_map {
  57. u16 encoding;
  58. u8 pic_mask;
  59. #define PIC_NONE 0x00
  60. #define PIC_UPPER 0x01
  61. #define PIC_LOWER 0x02
  62. };
  63. static unsigned long perf_event_encode(const struct perf_event_map *pmap)
  64. {
  65. return ((unsigned long) pmap->encoding << 16) | pmap->pic_mask;
  66. }
  67. static void perf_event_decode(unsigned long val, u16 *enc, u8 *msk)
  68. {
  69. *msk = val & 0xff;
  70. *enc = val >> 16;
  71. }
  72. #define C(x) PERF_COUNT_HW_CACHE_##x
  73. #define CACHE_OP_UNSUPPORTED 0xfffe
  74. #define CACHE_OP_NONSENSE 0xffff
  75. typedef struct perf_event_map cache_map_t
  76. [PERF_COUNT_HW_CACHE_MAX]
  77. [PERF_COUNT_HW_CACHE_OP_MAX]
  78. [PERF_COUNT_HW_CACHE_RESULT_MAX];
  79. struct sparc_pmu {
  80. const struct perf_event_map *(*event_map)(int);
  81. const cache_map_t *cache_map;
  82. int max_events;
  83. int upper_shift;
  84. int lower_shift;
  85. int event_mask;
  86. int hv_bit;
  87. int irq_bit;
  88. int upper_nop;
  89. int lower_nop;
  90. };
  91. static const struct perf_event_map ultra3_perfmon_event_map[] = {
  92. [PERF_COUNT_HW_CPU_CYCLES] = { 0x0000, PIC_UPPER | PIC_LOWER },
  93. [PERF_COUNT_HW_INSTRUCTIONS] = { 0x0001, PIC_UPPER | PIC_LOWER },
  94. [PERF_COUNT_HW_CACHE_REFERENCES] = { 0x0009, PIC_LOWER },
  95. [PERF_COUNT_HW_CACHE_MISSES] = { 0x0009, PIC_UPPER },
  96. };
  97. static const struct perf_event_map *ultra3_event_map(int event_id)
  98. {
  99. return &ultra3_perfmon_event_map[event_id];
  100. }
  101. static const cache_map_t ultra3_cache_map = {
  102. [C(L1D)] = {
  103. [C(OP_READ)] = {
  104. [C(RESULT_ACCESS)] = { 0x09, PIC_LOWER, },
  105. [C(RESULT_MISS)] = { 0x09, PIC_UPPER, },
  106. },
  107. [C(OP_WRITE)] = {
  108. [C(RESULT_ACCESS)] = { 0x0a, PIC_LOWER },
  109. [C(RESULT_MISS)] = { 0x0a, PIC_UPPER },
  110. },
  111. [C(OP_PREFETCH)] = {
  112. [C(RESULT_ACCESS)] = { CACHE_OP_UNSUPPORTED },
  113. [C(RESULT_MISS)] = { CACHE_OP_UNSUPPORTED },
  114. },
  115. },
  116. [C(L1I)] = {
  117. [C(OP_READ)] = {
  118. [C(RESULT_ACCESS)] = { 0x09, PIC_LOWER, },
  119. [C(RESULT_MISS)] = { 0x09, PIC_UPPER, },
  120. },
  121. [ C(OP_WRITE) ] = {
  122. [ C(RESULT_ACCESS) ] = { CACHE_OP_NONSENSE },
  123. [ C(RESULT_MISS) ] = { CACHE_OP_NONSENSE },
  124. },
  125. [ C(OP_PREFETCH) ] = {
  126. [ C(RESULT_ACCESS) ] = { CACHE_OP_UNSUPPORTED },
  127. [ C(RESULT_MISS) ] = { CACHE_OP_UNSUPPORTED },
  128. },
  129. },
  130. [C(LL)] = {
  131. [C(OP_READ)] = {
  132. [C(RESULT_ACCESS)] = { 0x0c, PIC_LOWER, },
  133. [C(RESULT_MISS)] = { 0x0c, PIC_UPPER, },
  134. },
  135. [C(OP_WRITE)] = {
  136. [C(RESULT_ACCESS)] = { 0x0c, PIC_LOWER },
  137. [C(RESULT_MISS)] = { 0x0c, PIC_UPPER },
  138. },
  139. [C(OP_PREFETCH)] = {
  140. [C(RESULT_ACCESS)] = { CACHE_OP_UNSUPPORTED },
  141. [C(RESULT_MISS)] = { CACHE_OP_UNSUPPORTED },
  142. },
  143. },
  144. [C(DTLB)] = {
  145. [C(OP_READ)] = {
  146. [C(RESULT_ACCESS)] = { CACHE_OP_UNSUPPORTED },
  147. [C(RESULT_MISS)] = { 0x12, PIC_UPPER, },
  148. },
  149. [ C(OP_WRITE) ] = {
  150. [ C(RESULT_ACCESS) ] = { CACHE_OP_UNSUPPORTED },
  151. [ C(RESULT_MISS) ] = { CACHE_OP_UNSUPPORTED },
  152. },
  153. [ C(OP_PREFETCH) ] = {
  154. [ C(RESULT_ACCESS) ] = { CACHE_OP_UNSUPPORTED },
  155. [ C(RESULT_MISS) ] = { CACHE_OP_UNSUPPORTED },
  156. },
  157. },
  158. [C(ITLB)] = {
  159. [C(OP_READ)] = {
  160. [C(RESULT_ACCESS)] = { CACHE_OP_UNSUPPORTED },
  161. [C(RESULT_MISS)] = { 0x11, PIC_UPPER, },
  162. },
  163. [ C(OP_WRITE) ] = {
  164. [ C(RESULT_ACCESS) ] = { CACHE_OP_UNSUPPORTED },
  165. [ C(RESULT_MISS) ] = { CACHE_OP_UNSUPPORTED },
  166. },
  167. [ C(OP_PREFETCH) ] = {
  168. [ C(RESULT_ACCESS) ] = { CACHE_OP_UNSUPPORTED },
  169. [ C(RESULT_MISS) ] = { CACHE_OP_UNSUPPORTED },
  170. },
  171. },
  172. [C(BPU)] = {
  173. [C(OP_READ)] = {
  174. [C(RESULT_ACCESS)] = { CACHE_OP_UNSUPPORTED },
  175. [C(RESULT_MISS)] = { CACHE_OP_UNSUPPORTED },
  176. },
  177. [ C(OP_WRITE) ] = {
  178. [ C(RESULT_ACCESS) ] = { CACHE_OP_UNSUPPORTED },
  179. [ C(RESULT_MISS) ] = { CACHE_OP_UNSUPPORTED },
  180. },
  181. [ C(OP_PREFETCH) ] = {
  182. [ C(RESULT_ACCESS) ] = { CACHE_OP_UNSUPPORTED },
  183. [ C(RESULT_MISS) ] = { CACHE_OP_UNSUPPORTED },
  184. },
  185. },
  186. };
  187. static const struct sparc_pmu ultra3_pmu = {
  188. .event_map = ultra3_event_map,
  189. .cache_map = &ultra3_cache_map,
  190. .max_events = ARRAY_SIZE(ultra3_perfmon_event_map),
  191. .upper_shift = 11,
  192. .lower_shift = 4,
  193. .event_mask = 0x3f,
  194. .upper_nop = 0x1c,
  195. .lower_nop = 0x14,
  196. };
  197. /* Niagara1 is very limited. The upper PIC is hard-locked to count
  198. * only instructions, so it is free running which creates all kinds of
  199. * problems. Some hardware designs make one wonder if the creator
  200. * even looked at how this stuff gets used by software.
  201. */
  202. static const struct perf_event_map niagara1_perfmon_event_map[] = {
  203. [PERF_COUNT_HW_CPU_CYCLES] = { 0x00, PIC_UPPER },
  204. [PERF_COUNT_HW_INSTRUCTIONS] = { 0x00, PIC_UPPER },
  205. [PERF_COUNT_HW_CACHE_REFERENCES] = { 0, PIC_NONE },
  206. [PERF_COUNT_HW_CACHE_MISSES] = { 0x03, PIC_LOWER },
  207. };
  208. static const struct perf_event_map *niagara1_event_map(int event_id)
  209. {
  210. return &niagara1_perfmon_event_map[event_id];
  211. }
  212. static const cache_map_t niagara1_cache_map = {
  213. [C(L1D)] = {
  214. [C(OP_READ)] = {
  215. [C(RESULT_ACCESS)] = { CACHE_OP_UNSUPPORTED },
  216. [C(RESULT_MISS)] = { 0x03, PIC_LOWER, },
  217. },
  218. [C(OP_WRITE)] = {
  219. [C(RESULT_ACCESS)] = { CACHE_OP_UNSUPPORTED },
  220. [C(RESULT_MISS)] = { 0x03, PIC_LOWER, },
  221. },
  222. [C(OP_PREFETCH)] = {
  223. [C(RESULT_ACCESS)] = { CACHE_OP_UNSUPPORTED },
  224. [C(RESULT_MISS)] = { CACHE_OP_UNSUPPORTED },
  225. },
  226. },
  227. [C(L1I)] = {
  228. [C(OP_READ)] = {
  229. [C(RESULT_ACCESS)] = { 0x00, PIC_UPPER },
  230. [C(RESULT_MISS)] = { 0x02, PIC_LOWER, },
  231. },
  232. [ C(OP_WRITE) ] = {
  233. [ C(RESULT_ACCESS) ] = { CACHE_OP_NONSENSE },
  234. [ C(RESULT_MISS) ] = { CACHE_OP_NONSENSE },
  235. },
  236. [ C(OP_PREFETCH) ] = {
  237. [ C(RESULT_ACCESS) ] = { CACHE_OP_UNSUPPORTED },
  238. [ C(RESULT_MISS) ] = { CACHE_OP_UNSUPPORTED },
  239. },
  240. },
  241. [C(LL)] = {
  242. [C(OP_READ)] = {
  243. [C(RESULT_ACCESS)] = { CACHE_OP_UNSUPPORTED },
  244. [C(RESULT_MISS)] = { 0x07, PIC_LOWER, },
  245. },
  246. [C(OP_WRITE)] = {
  247. [C(RESULT_ACCESS)] = { CACHE_OP_UNSUPPORTED },
  248. [C(RESULT_MISS)] = { 0x07, PIC_LOWER, },
  249. },
  250. [C(OP_PREFETCH)] = {
  251. [C(RESULT_ACCESS)] = { CACHE_OP_UNSUPPORTED },
  252. [C(RESULT_MISS)] = { CACHE_OP_UNSUPPORTED },
  253. },
  254. },
  255. [C(DTLB)] = {
  256. [C(OP_READ)] = {
  257. [C(RESULT_ACCESS)] = { CACHE_OP_UNSUPPORTED },
  258. [C(RESULT_MISS)] = { 0x05, PIC_LOWER, },
  259. },
  260. [ C(OP_WRITE) ] = {
  261. [ C(RESULT_ACCESS) ] = { CACHE_OP_UNSUPPORTED },
  262. [ C(RESULT_MISS) ] = { CACHE_OP_UNSUPPORTED },
  263. },
  264. [ C(OP_PREFETCH) ] = {
  265. [ C(RESULT_ACCESS) ] = { CACHE_OP_UNSUPPORTED },
  266. [ C(RESULT_MISS) ] = { CACHE_OP_UNSUPPORTED },
  267. },
  268. },
  269. [C(ITLB)] = {
  270. [C(OP_READ)] = {
  271. [C(RESULT_ACCESS)] = { CACHE_OP_UNSUPPORTED },
  272. [C(RESULT_MISS)] = { 0x04, PIC_LOWER, },
  273. },
  274. [ C(OP_WRITE) ] = {
  275. [ C(RESULT_ACCESS) ] = { CACHE_OP_UNSUPPORTED },
  276. [ C(RESULT_MISS) ] = { CACHE_OP_UNSUPPORTED },
  277. },
  278. [ C(OP_PREFETCH) ] = {
  279. [ C(RESULT_ACCESS) ] = { CACHE_OP_UNSUPPORTED },
  280. [ C(RESULT_MISS) ] = { CACHE_OP_UNSUPPORTED },
  281. },
  282. },
  283. [C(BPU)] = {
  284. [C(OP_READ)] = {
  285. [C(RESULT_ACCESS)] = { CACHE_OP_UNSUPPORTED },
  286. [C(RESULT_MISS)] = { CACHE_OP_UNSUPPORTED },
  287. },
  288. [ C(OP_WRITE) ] = {
  289. [ C(RESULT_ACCESS) ] = { CACHE_OP_UNSUPPORTED },
  290. [ C(RESULT_MISS) ] = { CACHE_OP_UNSUPPORTED },
  291. },
  292. [ C(OP_PREFETCH) ] = {
  293. [ C(RESULT_ACCESS) ] = { CACHE_OP_UNSUPPORTED },
  294. [ C(RESULT_MISS) ] = { CACHE_OP_UNSUPPORTED },
  295. },
  296. },
  297. };
  298. static const struct sparc_pmu niagara1_pmu = {
  299. .event_map = niagara1_event_map,
  300. .cache_map = &niagara1_cache_map,
  301. .max_events = ARRAY_SIZE(niagara1_perfmon_event_map),
  302. .upper_shift = 0,
  303. .lower_shift = 4,
  304. .event_mask = 0x7,
  305. .upper_nop = 0x0,
  306. .lower_nop = 0x0,
  307. };
  308. static const struct perf_event_map niagara2_perfmon_event_map[] = {
  309. [PERF_COUNT_HW_CPU_CYCLES] = { 0x02ff, PIC_UPPER | PIC_LOWER },
  310. [PERF_COUNT_HW_INSTRUCTIONS] = { 0x02ff, PIC_UPPER | PIC_LOWER },
  311. [PERF_COUNT_HW_CACHE_REFERENCES] = { 0x0208, PIC_UPPER | PIC_LOWER },
  312. [PERF_COUNT_HW_CACHE_MISSES] = { 0x0302, PIC_UPPER | PIC_LOWER },
  313. [PERF_COUNT_HW_BRANCH_INSTRUCTIONS] = { 0x0201, PIC_UPPER | PIC_LOWER },
  314. [PERF_COUNT_HW_BRANCH_MISSES] = { 0x0202, PIC_UPPER | PIC_LOWER },
  315. };
  316. static const struct perf_event_map *niagara2_event_map(int event_id)
  317. {
  318. return &niagara2_perfmon_event_map[event_id];
  319. }
  320. static const cache_map_t niagara2_cache_map = {
  321. [C(L1D)] = {
  322. [C(OP_READ)] = {
  323. [C(RESULT_ACCESS)] = { 0x0208, PIC_UPPER | PIC_LOWER, },
  324. [C(RESULT_MISS)] = { 0x0302, PIC_UPPER | PIC_LOWER, },
  325. },
  326. [C(OP_WRITE)] = {
  327. [C(RESULT_ACCESS)] = { 0x0210, PIC_UPPER | PIC_LOWER, },
  328. [C(RESULT_MISS)] = { 0x0302, PIC_UPPER | PIC_LOWER, },
  329. },
  330. [C(OP_PREFETCH)] = {
  331. [C(RESULT_ACCESS)] = { CACHE_OP_UNSUPPORTED },
  332. [C(RESULT_MISS)] = { CACHE_OP_UNSUPPORTED },
  333. },
  334. },
  335. [C(L1I)] = {
  336. [C(OP_READ)] = {
  337. [C(RESULT_ACCESS)] = { 0x02ff, PIC_UPPER | PIC_LOWER, },
  338. [C(RESULT_MISS)] = { 0x0301, PIC_UPPER | PIC_LOWER, },
  339. },
  340. [ C(OP_WRITE) ] = {
  341. [ C(RESULT_ACCESS) ] = { CACHE_OP_NONSENSE },
  342. [ C(RESULT_MISS) ] = { CACHE_OP_NONSENSE },
  343. },
  344. [ C(OP_PREFETCH) ] = {
  345. [ C(RESULT_ACCESS) ] = { CACHE_OP_UNSUPPORTED },
  346. [ C(RESULT_MISS) ] = { CACHE_OP_UNSUPPORTED },
  347. },
  348. },
  349. [C(LL)] = {
  350. [C(OP_READ)] = {
  351. [C(RESULT_ACCESS)] = { 0x0208, PIC_UPPER | PIC_LOWER, },
  352. [C(RESULT_MISS)] = { 0x0330, PIC_UPPER | PIC_LOWER, },
  353. },
  354. [C(OP_WRITE)] = {
  355. [C(RESULT_ACCESS)] = { 0x0210, PIC_UPPER | PIC_LOWER, },
  356. [C(RESULT_MISS)] = { 0x0320, PIC_UPPER | PIC_LOWER, },
  357. },
  358. [C(OP_PREFETCH)] = {
  359. [C(RESULT_ACCESS)] = { CACHE_OP_UNSUPPORTED },
  360. [C(RESULT_MISS)] = { CACHE_OP_UNSUPPORTED },
  361. },
  362. },
  363. [C(DTLB)] = {
  364. [C(OP_READ)] = {
  365. [C(RESULT_ACCESS)] = { CACHE_OP_UNSUPPORTED },
  366. [C(RESULT_MISS)] = { 0x0b08, PIC_UPPER | PIC_LOWER, },
  367. },
  368. [ C(OP_WRITE) ] = {
  369. [ C(RESULT_ACCESS) ] = { CACHE_OP_UNSUPPORTED },
  370. [ C(RESULT_MISS) ] = { CACHE_OP_UNSUPPORTED },
  371. },
  372. [ C(OP_PREFETCH) ] = {
  373. [ C(RESULT_ACCESS) ] = { CACHE_OP_UNSUPPORTED },
  374. [ C(RESULT_MISS) ] = { CACHE_OP_UNSUPPORTED },
  375. },
  376. },
  377. [C(ITLB)] = {
  378. [C(OP_READ)] = {
  379. [C(RESULT_ACCESS)] = { CACHE_OP_UNSUPPORTED },
  380. [C(RESULT_MISS)] = { 0xb04, PIC_UPPER | PIC_LOWER, },
  381. },
  382. [ C(OP_WRITE) ] = {
  383. [ C(RESULT_ACCESS) ] = { CACHE_OP_UNSUPPORTED },
  384. [ C(RESULT_MISS) ] = { CACHE_OP_UNSUPPORTED },
  385. },
  386. [ C(OP_PREFETCH) ] = {
  387. [ C(RESULT_ACCESS) ] = { CACHE_OP_UNSUPPORTED },
  388. [ C(RESULT_MISS) ] = { CACHE_OP_UNSUPPORTED },
  389. },
  390. },
  391. [C(BPU)] = {
  392. [C(OP_READ)] = {
  393. [C(RESULT_ACCESS)] = { CACHE_OP_UNSUPPORTED },
  394. [C(RESULT_MISS)] = { CACHE_OP_UNSUPPORTED },
  395. },
  396. [ C(OP_WRITE) ] = {
  397. [ C(RESULT_ACCESS) ] = { CACHE_OP_UNSUPPORTED },
  398. [ C(RESULT_MISS) ] = { CACHE_OP_UNSUPPORTED },
  399. },
  400. [ C(OP_PREFETCH) ] = {
  401. [ C(RESULT_ACCESS) ] = { CACHE_OP_UNSUPPORTED },
  402. [ C(RESULT_MISS) ] = { CACHE_OP_UNSUPPORTED },
  403. },
  404. },
  405. };
  406. static const struct sparc_pmu niagara2_pmu = {
  407. .event_map = niagara2_event_map,
  408. .cache_map = &niagara2_cache_map,
  409. .max_events = ARRAY_SIZE(niagara2_perfmon_event_map),
  410. .upper_shift = 19,
  411. .lower_shift = 6,
  412. .event_mask = 0xfff,
  413. .hv_bit = 0x8,
  414. .irq_bit = 0x03,
  415. .upper_nop = 0x220,
  416. .lower_nop = 0x220,
  417. };
  418. static const struct sparc_pmu *sparc_pmu __read_mostly;
  419. static u64 event_encoding(u64 event_id, int idx)
  420. {
  421. if (idx == PIC_UPPER_INDEX)
  422. event_id <<= sparc_pmu->upper_shift;
  423. else
  424. event_id <<= sparc_pmu->lower_shift;
  425. return event_id;
  426. }
  427. static u64 mask_for_index(int idx)
  428. {
  429. return event_encoding(sparc_pmu->event_mask, idx);
  430. }
  431. static u64 nop_for_index(int idx)
  432. {
  433. return event_encoding(idx == PIC_UPPER_INDEX ?
  434. sparc_pmu->upper_nop :
  435. sparc_pmu->lower_nop, idx);
  436. }
  437. static inline void sparc_pmu_enable_event(struct hw_perf_event *hwc, int idx)
  438. {
  439. u64 val, mask = mask_for_index(idx);
  440. val = pcr_ops->read();
  441. pcr_ops->write((val & ~mask) | hwc->config);
  442. }
  443. static inline void sparc_pmu_disable_event(struct hw_perf_event *hwc, int idx)
  444. {
  445. u64 mask = mask_for_index(idx);
  446. u64 nop = nop_for_index(idx);
  447. u64 val = pcr_ops->read();
  448. pcr_ops->write((val & ~mask) | nop);
  449. }
  450. void hw_perf_enable(void)
  451. {
  452. struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
  453. u64 val;
  454. int i;
  455. if (cpuc->enabled)
  456. return;
  457. cpuc->enabled = 1;
  458. barrier();
  459. val = pcr_ops->read();
  460. for (i = 0; i < MAX_HWEVENTS; i++) {
  461. struct perf_event *cp = cpuc->events[i];
  462. struct hw_perf_event *hwc;
  463. if (!cp)
  464. continue;
  465. hwc = &cp->hw;
  466. val |= hwc->config_base;
  467. }
  468. pcr_ops->write(val);
  469. }
  470. void hw_perf_disable(void)
  471. {
  472. struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
  473. u64 val;
  474. if (!cpuc->enabled)
  475. return;
  476. cpuc->enabled = 0;
  477. val = pcr_ops->read();
  478. val &= ~(PCR_UTRACE | PCR_STRACE |
  479. sparc_pmu->hv_bit | sparc_pmu->irq_bit);
  480. pcr_ops->write(val);
  481. }
  482. static u32 read_pmc(int idx)
  483. {
  484. u64 val;
  485. read_pic(val);
  486. if (idx == PIC_UPPER_INDEX)
  487. val >>= 32;
  488. return val & 0xffffffff;
  489. }
  490. static void write_pmc(int idx, u64 val)
  491. {
  492. u64 shift, mask, pic;
  493. shift = 0;
  494. if (idx == PIC_UPPER_INDEX)
  495. shift = 32;
  496. mask = ((u64) 0xffffffff) << shift;
  497. val <<= shift;
  498. read_pic(pic);
  499. pic &= ~mask;
  500. pic |= val;
  501. write_pic(pic);
  502. }
  503. static int sparc_perf_event_set_period(struct perf_event *event,
  504. struct hw_perf_event *hwc, int idx)
  505. {
  506. s64 left = atomic64_read(&hwc->period_left);
  507. s64 period = hwc->sample_period;
  508. int ret = 0;
  509. if (unlikely(left <= -period)) {
  510. left = period;
  511. atomic64_set(&hwc->period_left, left);
  512. hwc->last_period = period;
  513. ret = 1;
  514. }
  515. if (unlikely(left <= 0)) {
  516. left += period;
  517. atomic64_set(&hwc->period_left, left);
  518. hwc->last_period = period;
  519. ret = 1;
  520. }
  521. if (left > MAX_PERIOD)
  522. left = MAX_PERIOD;
  523. atomic64_set(&hwc->prev_count, (u64)-left);
  524. write_pmc(idx, (u64)(-left) & 0xffffffff);
  525. perf_event_update_userpage(event);
  526. return ret;
  527. }
  528. static int sparc_pmu_enable(struct perf_event *event)
  529. {
  530. struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
  531. struct hw_perf_event *hwc = &event->hw;
  532. int idx = hwc->idx;
  533. if (test_and_set_bit(idx, cpuc->used_mask))
  534. return -EAGAIN;
  535. sparc_pmu_disable_event(hwc, idx);
  536. cpuc->events[idx] = event;
  537. set_bit(idx, cpuc->active_mask);
  538. sparc_perf_event_set_period(event, hwc, idx);
  539. sparc_pmu_enable_event(hwc, idx);
  540. perf_event_update_userpage(event);
  541. return 0;
  542. }
  543. static u64 sparc_perf_event_update(struct perf_event *event,
  544. struct hw_perf_event *hwc, int idx)
  545. {
  546. int shift = 64 - 32;
  547. u64 prev_raw_count, new_raw_count;
  548. s64 delta;
  549. again:
  550. prev_raw_count = atomic64_read(&hwc->prev_count);
  551. new_raw_count = read_pmc(idx);
  552. if (atomic64_cmpxchg(&hwc->prev_count, prev_raw_count,
  553. new_raw_count) != prev_raw_count)
  554. goto again;
  555. delta = (new_raw_count << shift) - (prev_raw_count << shift);
  556. delta >>= shift;
  557. atomic64_add(delta, &event->count);
  558. atomic64_sub(delta, &hwc->period_left);
  559. return new_raw_count;
  560. }
  561. static void sparc_pmu_disable(struct perf_event *event)
  562. {
  563. struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
  564. struct hw_perf_event *hwc = &event->hw;
  565. int idx = hwc->idx;
  566. clear_bit(idx, cpuc->active_mask);
  567. sparc_pmu_disable_event(hwc, idx);
  568. barrier();
  569. sparc_perf_event_update(event, hwc, idx);
  570. cpuc->events[idx] = NULL;
  571. clear_bit(idx, cpuc->used_mask);
  572. perf_event_update_userpage(event);
  573. }
  574. static void sparc_pmu_read(struct perf_event *event)
  575. {
  576. struct hw_perf_event *hwc = &event->hw;
  577. sparc_perf_event_update(event, hwc, hwc->idx);
  578. }
  579. static void sparc_pmu_unthrottle(struct perf_event *event)
  580. {
  581. struct hw_perf_event *hwc = &event->hw;
  582. sparc_pmu_enable_event(hwc, hwc->idx);
  583. }
  584. static atomic_t active_events = ATOMIC_INIT(0);
  585. static DEFINE_MUTEX(pmc_grab_mutex);
  586. void perf_event_grab_pmc(void)
  587. {
  588. if (atomic_inc_not_zero(&active_events))
  589. return;
  590. mutex_lock(&pmc_grab_mutex);
  591. if (atomic_read(&active_events) == 0) {
  592. if (atomic_read(&nmi_active) > 0) {
  593. on_each_cpu(stop_nmi_watchdog, NULL, 1);
  594. BUG_ON(atomic_read(&nmi_active) != 0);
  595. }
  596. atomic_inc(&active_events);
  597. }
  598. mutex_unlock(&pmc_grab_mutex);
  599. }
  600. void perf_event_release_pmc(void)
  601. {
  602. if (atomic_dec_and_mutex_lock(&active_events, &pmc_grab_mutex)) {
  603. if (atomic_read(&nmi_active) == 0)
  604. on_each_cpu(start_nmi_watchdog, NULL, 1);
  605. mutex_unlock(&pmc_grab_mutex);
  606. }
  607. }
  608. static const struct perf_event_map *sparc_map_cache_event(u64 config)
  609. {
  610. unsigned int cache_type, cache_op, cache_result;
  611. const struct perf_event_map *pmap;
  612. if (!sparc_pmu->cache_map)
  613. return ERR_PTR(-ENOENT);
  614. cache_type = (config >> 0) & 0xff;
  615. if (cache_type >= PERF_COUNT_HW_CACHE_MAX)
  616. return ERR_PTR(-EINVAL);
  617. cache_op = (config >> 8) & 0xff;
  618. if (cache_op >= PERF_COUNT_HW_CACHE_OP_MAX)
  619. return ERR_PTR(-EINVAL);
  620. cache_result = (config >> 16) & 0xff;
  621. if (cache_result >= PERF_COUNT_HW_CACHE_RESULT_MAX)
  622. return ERR_PTR(-EINVAL);
  623. pmap = &((*sparc_pmu->cache_map)[cache_type][cache_op][cache_result]);
  624. if (pmap->encoding == CACHE_OP_UNSUPPORTED)
  625. return ERR_PTR(-ENOENT);
  626. if (pmap->encoding == CACHE_OP_NONSENSE)
  627. return ERR_PTR(-EINVAL);
  628. return pmap;
  629. }
  630. static void hw_perf_event_destroy(struct perf_event *event)
  631. {
  632. perf_event_release_pmc();
  633. }
  634. /* Make sure all events can be scheduled into the hardware at
  635. * the same time. This is simplified by the fact that we only
  636. * need to support 2 simultaneous HW events.
  637. */
  638. static int sparc_check_constraints(unsigned long *events, int n_ev)
  639. {
  640. if (n_ev <= perf_max_events) {
  641. u8 msk1, msk2;
  642. u16 dummy;
  643. if (n_ev == 1)
  644. return 0;
  645. BUG_ON(n_ev != 2);
  646. perf_event_decode(events[0], &dummy, &msk1);
  647. perf_event_decode(events[1], &dummy, &msk2);
  648. /* If both events can go on any counter, OK. */
  649. if (msk1 == (PIC_UPPER | PIC_LOWER) &&
  650. msk2 == (PIC_UPPER | PIC_LOWER))
  651. return 0;
  652. /* If one event is limited to a specific counter,
  653. * and the other can go on both, OK.
  654. */
  655. if ((msk1 == PIC_UPPER || msk1 == PIC_LOWER) &&
  656. msk2 == (PIC_UPPER | PIC_LOWER))
  657. return 0;
  658. if ((msk2 == PIC_UPPER || msk2 == PIC_LOWER) &&
  659. msk1 == (PIC_UPPER | PIC_LOWER))
  660. return 0;
  661. /* If the events are fixed to different counters, OK. */
  662. if ((msk1 == PIC_UPPER && msk2 == PIC_LOWER) ||
  663. (msk1 == PIC_LOWER && msk2 == PIC_UPPER))
  664. return 0;
  665. /* Otherwise, there is a conflict. */
  666. }
  667. return -1;
  668. }
  669. static int check_excludes(struct perf_event **evts, int n_prev, int n_new)
  670. {
  671. int eu = 0, ek = 0, eh = 0;
  672. struct perf_event *event;
  673. int i, n, first;
  674. n = n_prev + n_new;
  675. if (n <= 1)
  676. return 0;
  677. first = 1;
  678. for (i = 0; i < n; i++) {
  679. event = evts[i];
  680. if (first) {
  681. eu = event->attr.exclude_user;
  682. ek = event->attr.exclude_kernel;
  683. eh = event->attr.exclude_hv;
  684. first = 0;
  685. } else if (event->attr.exclude_user != eu ||
  686. event->attr.exclude_kernel != ek ||
  687. event->attr.exclude_hv != eh) {
  688. return -EAGAIN;
  689. }
  690. }
  691. return 0;
  692. }
  693. static int collect_events(struct perf_event *group, int max_count,
  694. struct perf_event *evts[], unsigned long *events)
  695. {
  696. struct perf_event *event;
  697. int n = 0;
  698. if (!is_software_event(group)) {
  699. if (n >= max_count)
  700. return -1;
  701. evts[n] = group;
  702. events[n++] = group->hw.event_base;
  703. }
  704. list_for_each_entry(event, &group->sibling_list, group_entry) {
  705. if (!is_software_event(event) &&
  706. event->state != PERF_EVENT_STATE_OFF) {
  707. if (n >= max_count)
  708. return -1;
  709. evts[n] = event;
  710. events[n++] = event->hw.event_base;
  711. }
  712. }
  713. return n;
  714. }
  715. static int __hw_perf_event_init(struct perf_event *event)
  716. {
  717. struct perf_event_attr *attr = &event->attr;
  718. struct perf_event *evts[MAX_HWEVENTS];
  719. struct hw_perf_event *hwc = &event->hw;
  720. unsigned long events[MAX_HWEVENTS];
  721. const struct perf_event_map *pmap;
  722. u64 enc;
  723. int n;
  724. if (atomic_read(&nmi_active) < 0)
  725. return -ENODEV;
  726. if (attr->type == PERF_TYPE_HARDWARE) {
  727. if (attr->config >= sparc_pmu->max_events)
  728. return -EINVAL;
  729. pmap = sparc_pmu->event_map(attr->config);
  730. } else if (attr->type == PERF_TYPE_HW_CACHE) {
  731. pmap = sparc_map_cache_event(attr->config);
  732. if (IS_ERR(pmap))
  733. return PTR_ERR(pmap);
  734. } else
  735. return -EOPNOTSUPP;
  736. /* We save the enable bits in the config_base. So to
  737. * turn off sampling just write 'config', and to enable
  738. * things write 'config | config_base'.
  739. */
  740. hwc->config_base = sparc_pmu->irq_bit;
  741. if (!attr->exclude_user)
  742. hwc->config_base |= PCR_UTRACE;
  743. if (!attr->exclude_kernel)
  744. hwc->config_base |= PCR_STRACE;
  745. if (!attr->exclude_hv)
  746. hwc->config_base |= sparc_pmu->hv_bit;
  747. hwc->event_base = perf_event_encode(pmap);
  748. enc = pmap->encoding;
  749. n = 0;
  750. if (event->group_leader != event) {
  751. n = collect_events(event->group_leader,
  752. perf_max_events - 1,
  753. evts, events);
  754. if (n < 0)
  755. return -EINVAL;
  756. }
  757. events[n] = hwc->event_base;
  758. evts[n] = event;
  759. if (check_excludes(evts, n, 1))
  760. return -EINVAL;
  761. if (sparc_check_constraints(events, n + 1))
  762. return -EINVAL;
  763. /* Try to do all error checking before this point, as unwinding
  764. * state after grabbing the PMC is difficult.
  765. */
  766. perf_event_grab_pmc();
  767. event->destroy = hw_perf_event_destroy;
  768. if (!hwc->sample_period) {
  769. hwc->sample_period = MAX_PERIOD;
  770. hwc->last_period = hwc->sample_period;
  771. atomic64_set(&hwc->period_left, hwc->sample_period);
  772. }
  773. if (pmap->pic_mask & PIC_UPPER) {
  774. hwc->idx = PIC_UPPER_INDEX;
  775. enc <<= sparc_pmu->upper_shift;
  776. } else {
  777. hwc->idx = PIC_LOWER_INDEX;
  778. enc <<= sparc_pmu->lower_shift;
  779. }
  780. hwc->config |= enc;
  781. return 0;
  782. }
  783. static const struct pmu pmu = {
  784. .enable = sparc_pmu_enable,
  785. .disable = sparc_pmu_disable,
  786. .read = sparc_pmu_read,
  787. .unthrottle = sparc_pmu_unthrottle,
  788. };
  789. const struct pmu *hw_perf_event_init(struct perf_event *event)
  790. {
  791. int err = __hw_perf_event_init(event);
  792. if (err)
  793. return ERR_PTR(err);
  794. return &pmu;
  795. }
  796. void perf_event_print_debug(void)
  797. {
  798. unsigned long flags;
  799. u64 pcr, pic;
  800. int cpu;
  801. if (!sparc_pmu)
  802. return;
  803. local_irq_save(flags);
  804. cpu = smp_processor_id();
  805. pcr = pcr_ops->read();
  806. read_pic(pic);
  807. pr_info("\n");
  808. pr_info("CPU#%d: PCR[%016llx] PIC[%016llx]\n",
  809. cpu, pcr, pic);
  810. local_irq_restore(flags);
  811. }
  812. static int __kprobes perf_event_nmi_handler(struct notifier_block *self,
  813. unsigned long cmd, void *__args)
  814. {
  815. struct die_args *args = __args;
  816. struct perf_sample_data data;
  817. struct cpu_hw_events *cpuc;
  818. struct pt_regs *regs;
  819. int idx;
  820. if (!atomic_read(&active_events))
  821. return NOTIFY_DONE;
  822. switch (cmd) {
  823. case DIE_NMI:
  824. break;
  825. default:
  826. return NOTIFY_DONE;
  827. }
  828. regs = args->regs;
  829. data.addr = 0;
  830. cpuc = &__get_cpu_var(cpu_hw_events);
  831. for (idx = 0; idx < MAX_HWEVENTS; idx++) {
  832. struct perf_event *event = cpuc->events[idx];
  833. struct hw_perf_event *hwc;
  834. u64 val;
  835. if (!test_bit(idx, cpuc->active_mask))
  836. continue;
  837. hwc = &event->hw;
  838. val = sparc_perf_event_update(event, hwc, idx);
  839. if (val & (1ULL << 31))
  840. continue;
  841. data.period = event->hw.last_period;
  842. if (!sparc_perf_event_set_period(event, hwc, idx))
  843. continue;
  844. if (perf_event_overflow(event, 1, &data, regs))
  845. sparc_pmu_disable_event(hwc, idx);
  846. }
  847. return NOTIFY_STOP;
  848. }
  849. static __read_mostly struct notifier_block perf_event_nmi_notifier = {
  850. .notifier_call = perf_event_nmi_handler,
  851. };
  852. static bool __init supported_pmu(void)
  853. {
  854. if (!strcmp(sparc_pmu_type, "ultra3") ||
  855. !strcmp(sparc_pmu_type, "ultra3+") ||
  856. !strcmp(sparc_pmu_type, "ultra3i") ||
  857. !strcmp(sparc_pmu_type, "ultra4+")) {
  858. sparc_pmu = &ultra3_pmu;
  859. return true;
  860. }
  861. if (!strcmp(sparc_pmu_type, "niagara")) {
  862. sparc_pmu = &niagara1_pmu;
  863. return true;
  864. }
  865. if (!strcmp(sparc_pmu_type, "niagara2")) {
  866. sparc_pmu = &niagara2_pmu;
  867. return true;
  868. }
  869. return false;
  870. }
  871. void __init init_hw_perf_events(void)
  872. {
  873. pr_info("Performance events: ");
  874. if (!supported_pmu()) {
  875. pr_cont("No support for PMU type '%s'\n", sparc_pmu_type);
  876. return;
  877. }
  878. pr_cont("Supported PMU type is '%s'\n", sparc_pmu_type);
  879. /* All sparc64 PMUs currently have 2 events. But this simple
  880. * driver only supports one active event at a time.
  881. */
  882. perf_max_events = 1;
  883. register_die_notifier(&perf_event_nmi_notifier);
  884. }