perf_event.c 26 KB

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