perf_event.c 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381
  1. /* Performance event support for sparc64.
  2. *
  3. * Copyright (C) 2009, 2010 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/stacktrace.h>
  20. #include <asm/cpudata.h>
  21. #include <asm/uaccess.h>
  22. #include <asm/atomic.h>
  23. #include <asm/nmi.h>
  24. #include <asm/pcr.h>
  25. #include "kstack.h"
  26. /* Sparc64 chips have two performance counters, 32-bits each, with
  27. * overflow interrupts generated on transition from 0xffffffff to 0.
  28. * The counters are accessed in one go using a 64-bit register.
  29. *
  30. * Both counters are controlled using a single control register. The
  31. * only way to stop all sampling is to clear all of the context (user,
  32. * supervisor, hypervisor) sampling enable bits. But these bits apply
  33. * to both counters, thus the two counters can't be enabled/disabled
  34. * individually.
  35. *
  36. * The control register has two event fields, one for each of the two
  37. * counters. It's thus nearly impossible to have one counter going
  38. * while keeping the other one stopped. Therefore it is possible to
  39. * get overflow interrupts for counters not currently "in use" and
  40. * that condition must be checked in the overflow interrupt handler.
  41. *
  42. * So we use a hack, in that we program inactive counters with the
  43. * "sw_count0" and "sw_count1" events. These count how many times
  44. * the instruction "sethi %hi(0xfc000), %g0" is executed. It's an
  45. * unusual way to encode a NOP and therefore will not trigger in
  46. * normal code.
  47. */
  48. #define MAX_HWEVENTS 2
  49. #define MAX_PERIOD ((1UL << 32) - 1)
  50. #define PIC_UPPER_INDEX 0
  51. #define PIC_LOWER_INDEX 1
  52. #define PIC_NO_INDEX -1
  53. struct cpu_hw_events {
  54. /* Number of events currently scheduled onto this cpu.
  55. * This tells how many entries in the arrays below
  56. * are valid.
  57. */
  58. int n_events;
  59. /* Number of new events added since the last hw_perf_disable().
  60. * This works because the perf event layer always adds new
  61. * events inside of a perf_{disable,enable}() sequence.
  62. */
  63. int n_added;
  64. /* Array of events current scheduled on this cpu. */
  65. struct perf_event *event[MAX_HWEVENTS];
  66. /* Array of encoded longs, specifying the %pcr register
  67. * encoding and the mask of PIC counters this even can
  68. * be scheduled on. See perf_event_encode() et al.
  69. */
  70. unsigned long events[MAX_HWEVENTS];
  71. /* The current counter index assigned to an event. When the
  72. * event hasn't been programmed into the cpu yet, this will
  73. * hold PIC_NO_INDEX. The event->hw.idx value tells us where
  74. * we ought to schedule the event.
  75. */
  76. int current_idx[MAX_HWEVENTS];
  77. /* Software copy of %pcr register on this cpu. */
  78. u64 pcr;
  79. /* Enabled/disable state. */
  80. int enabled;
  81. };
  82. DEFINE_PER_CPU(struct cpu_hw_events, cpu_hw_events) = { .enabled = 1, };
  83. /* An event map describes the characteristics of a performance
  84. * counter event. In particular it gives the encoding as well as
  85. * a mask telling which counters the event can be measured on.
  86. */
  87. struct perf_event_map {
  88. u16 encoding;
  89. u8 pic_mask;
  90. #define PIC_NONE 0x00
  91. #define PIC_UPPER 0x01
  92. #define PIC_LOWER 0x02
  93. };
  94. /* Encode a perf_event_map entry into a long. */
  95. static unsigned long perf_event_encode(const struct perf_event_map *pmap)
  96. {
  97. return ((unsigned long) pmap->encoding << 16) | pmap->pic_mask;
  98. }
  99. static u8 perf_event_get_msk(unsigned long val)
  100. {
  101. return val & 0xff;
  102. }
  103. static u64 perf_event_get_enc(unsigned long val)
  104. {
  105. return val >> 16;
  106. }
  107. #define C(x) PERF_COUNT_HW_CACHE_##x
  108. #define CACHE_OP_UNSUPPORTED 0xfffe
  109. #define CACHE_OP_NONSENSE 0xffff
  110. typedef struct perf_event_map cache_map_t
  111. [PERF_COUNT_HW_CACHE_MAX]
  112. [PERF_COUNT_HW_CACHE_OP_MAX]
  113. [PERF_COUNT_HW_CACHE_RESULT_MAX];
  114. struct sparc_pmu {
  115. const struct perf_event_map *(*event_map)(int);
  116. const cache_map_t *cache_map;
  117. int max_events;
  118. int upper_shift;
  119. int lower_shift;
  120. int event_mask;
  121. int hv_bit;
  122. int irq_bit;
  123. int upper_nop;
  124. int lower_nop;
  125. };
  126. static const struct perf_event_map ultra3_perfmon_event_map[] = {
  127. [PERF_COUNT_HW_CPU_CYCLES] = { 0x0000, PIC_UPPER | PIC_LOWER },
  128. [PERF_COUNT_HW_INSTRUCTIONS] = { 0x0001, PIC_UPPER | PIC_LOWER },
  129. [PERF_COUNT_HW_CACHE_REFERENCES] = { 0x0009, PIC_LOWER },
  130. [PERF_COUNT_HW_CACHE_MISSES] = { 0x0009, PIC_UPPER },
  131. };
  132. static const struct perf_event_map *ultra3_event_map(int event_id)
  133. {
  134. return &ultra3_perfmon_event_map[event_id];
  135. }
  136. static const cache_map_t ultra3_cache_map = {
  137. [C(L1D)] = {
  138. [C(OP_READ)] = {
  139. [C(RESULT_ACCESS)] = { 0x09, PIC_LOWER, },
  140. [C(RESULT_MISS)] = { 0x09, PIC_UPPER, },
  141. },
  142. [C(OP_WRITE)] = {
  143. [C(RESULT_ACCESS)] = { 0x0a, PIC_LOWER },
  144. [C(RESULT_MISS)] = { 0x0a, PIC_UPPER },
  145. },
  146. [C(OP_PREFETCH)] = {
  147. [C(RESULT_ACCESS)] = { CACHE_OP_UNSUPPORTED },
  148. [C(RESULT_MISS)] = { CACHE_OP_UNSUPPORTED },
  149. },
  150. },
  151. [C(L1I)] = {
  152. [C(OP_READ)] = {
  153. [C(RESULT_ACCESS)] = { 0x09, PIC_LOWER, },
  154. [C(RESULT_MISS)] = { 0x09, PIC_UPPER, },
  155. },
  156. [ C(OP_WRITE) ] = {
  157. [ C(RESULT_ACCESS) ] = { CACHE_OP_NONSENSE },
  158. [ C(RESULT_MISS) ] = { CACHE_OP_NONSENSE },
  159. },
  160. [ C(OP_PREFETCH) ] = {
  161. [ C(RESULT_ACCESS) ] = { CACHE_OP_UNSUPPORTED },
  162. [ C(RESULT_MISS) ] = { CACHE_OP_UNSUPPORTED },
  163. },
  164. },
  165. [C(LL)] = {
  166. [C(OP_READ)] = {
  167. [C(RESULT_ACCESS)] = { 0x0c, PIC_LOWER, },
  168. [C(RESULT_MISS)] = { 0x0c, PIC_UPPER, },
  169. },
  170. [C(OP_WRITE)] = {
  171. [C(RESULT_ACCESS)] = { 0x0c, PIC_LOWER },
  172. [C(RESULT_MISS)] = { 0x0c, PIC_UPPER },
  173. },
  174. [C(OP_PREFETCH)] = {
  175. [C(RESULT_ACCESS)] = { CACHE_OP_UNSUPPORTED },
  176. [C(RESULT_MISS)] = { CACHE_OP_UNSUPPORTED },
  177. },
  178. },
  179. [C(DTLB)] = {
  180. [C(OP_READ)] = {
  181. [C(RESULT_ACCESS)] = { CACHE_OP_UNSUPPORTED },
  182. [C(RESULT_MISS)] = { 0x12, PIC_UPPER, },
  183. },
  184. [ C(OP_WRITE) ] = {
  185. [ C(RESULT_ACCESS) ] = { CACHE_OP_UNSUPPORTED },
  186. [ C(RESULT_MISS) ] = { CACHE_OP_UNSUPPORTED },
  187. },
  188. [ C(OP_PREFETCH) ] = {
  189. [ C(RESULT_ACCESS) ] = { CACHE_OP_UNSUPPORTED },
  190. [ C(RESULT_MISS) ] = { CACHE_OP_UNSUPPORTED },
  191. },
  192. },
  193. [C(ITLB)] = {
  194. [C(OP_READ)] = {
  195. [C(RESULT_ACCESS)] = { CACHE_OP_UNSUPPORTED },
  196. [C(RESULT_MISS)] = { 0x11, PIC_UPPER, },
  197. },
  198. [ C(OP_WRITE) ] = {
  199. [ C(RESULT_ACCESS) ] = { CACHE_OP_UNSUPPORTED },
  200. [ C(RESULT_MISS) ] = { CACHE_OP_UNSUPPORTED },
  201. },
  202. [ C(OP_PREFETCH) ] = {
  203. [ C(RESULT_ACCESS) ] = { CACHE_OP_UNSUPPORTED },
  204. [ C(RESULT_MISS) ] = { CACHE_OP_UNSUPPORTED },
  205. },
  206. },
  207. [C(BPU)] = {
  208. [C(OP_READ)] = {
  209. [C(RESULT_ACCESS)] = { CACHE_OP_UNSUPPORTED },
  210. [C(RESULT_MISS)] = { CACHE_OP_UNSUPPORTED },
  211. },
  212. [ C(OP_WRITE) ] = {
  213. [ C(RESULT_ACCESS) ] = { CACHE_OP_UNSUPPORTED },
  214. [ C(RESULT_MISS) ] = { CACHE_OP_UNSUPPORTED },
  215. },
  216. [ C(OP_PREFETCH) ] = {
  217. [ C(RESULT_ACCESS) ] = { CACHE_OP_UNSUPPORTED },
  218. [ C(RESULT_MISS) ] = { CACHE_OP_UNSUPPORTED },
  219. },
  220. },
  221. };
  222. static const struct sparc_pmu ultra3_pmu = {
  223. .event_map = ultra3_event_map,
  224. .cache_map = &ultra3_cache_map,
  225. .max_events = ARRAY_SIZE(ultra3_perfmon_event_map),
  226. .upper_shift = 11,
  227. .lower_shift = 4,
  228. .event_mask = 0x3f,
  229. .upper_nop = 0x1c,
  230. .lower_nop = 0x14,
  231. };
  232. /* Niagara1 is very limited. The upper PIC is hard-locked to count
  233. * only instructions, so it is free running which creates all kinds of
  234. * problems. Some hardware designs make one wonder if the creator
  235. * even looked at how this stuff gets used by software.
  236. */
  237. static const struct perf_event_map niagara1_perfmon_event_map[] = {
  238. [PERF_COUNT_HW_CPU_CYCLES] = { 0x00, PIC_UPPER },
  239. [PERF_COUNT_HW_INSTRUCTIONS] = { 0x00, PIC_UPPER },
  240. [PERF_COUNT_HW_CACHE_REFERENCES] = { 0, PIC_NONE },
  241. [PERF_COUNT_HW_CACHE_MISSES] = { 0x03, PIC_LOWER },
  242. };
  243. static const struct perf_event_map *niagara1_event_map(int event_id)
  244. {
  245. return &niagara1_perfmon_event_map[event_id];
  246. }
  247. static const cache_map_t niagara1_cache_map = {
  248. [C(L1D)] = {
  249. [C(OP_READ)] = {
  250. [C(RESULT_ACCESS)] = { CACHE_OP_UNSUPPORTED },
  251. [C(RESULT_MISS)] = { 0x03, PIC_LOWER, },
  252. },
  253. [C(OP_WRITE)] = {
  254. [C(RESULT_ACCESS)] = { CACHE_OP_UNSUPPORTED },
  255. [C(RESULT_MISS)] = { 0x03, PIC_LOWER, },
  256. },
  257. [C(OP_PREFETCH)] = {
  258. [C(RESULT_ACCESS)] = { CACHE_OP_UNSUPPORTED },
  259. [C(RESULT_MISS)] = { CACHE_OP_UNSUPPORTED },
  260. },
  261. },
  262. [C(L1I)] = {
  263. [C(OP_READ)] = {
  264. [C(RESULT_ACCESS)] = { 0x00, PIC_UPPER },
  265. [C(RESULT_MISS)] = { 0x02, PIC_LOWER, },
  266. },
  267. [ C(OP_WRITE) ] = {
  268. [ C(RESULT_ACCESS) ] = { CACHE_OP_NONSENSE },
  269. [ C(RESULT_MISS) ] = { CACHE_OP_NONSENSE },
  270. },
  271. [ C(OP_PREFETCH) ] = {
  272. [ C(RESULT_ACCESS) ] = { CACHE_OP_UNSUPPORTED },
  273. [ C(RESULT_MISS) ] = { CACHE_OP_UNSUPPORTED },
  274. },
  275. },
  276. [C(LL)] = {
  277. [C(OP_READ)] = {
  278. [C(RESULT_ACCESS)] = { CACHE_OP_UNSUPPORTED },
  279. [C(RESULT_MISS)] = { 0x07, PIC_LOWER, },
  280. },
  281. [C(OP_WRITE)] = {
  282. [C(RESULT_ACCESS)] = { CACHE_OP_UNSUPPORTED },
  283. [C(RESULT_MISS)] = { 0x07, PIC_LOWER, },
  284. },
  285. [C(OP_PREFETCH)] = {
  286. [C(RESULT_ACCESS)] = { CACHE_OP_UNSUPPORTED },
  287. [C(RESULT_MISS)] = { CACHE_OP_UNSUPPORTED },
  288. },
  289. },
  290. [C(DTLB)] = {
  291. [C(OP_READ)] = {
  292. [C(RESULT_ACCESS)] = { CACHE_OP_UNSUPPORTED },
  293. [C(RESULT_MISS)] = { 0x05, PIC_LOWER, },
  294. },
  295. [ C(OP_WRITE) ] = {
  296. [ C(RESULT_ACCESS) ] = { CACHE_OP_UNSUPPORTED },
  297. [ C(RESULT_MISS) ] = { CACHE_OP_UNSUPPORTED },
  298. },
  299. [ C(OP_PREFETCH) ] = {
  300. [ C(RESULT_ACCESS) ] = { CACHE_OP_UNSUPPORTED },
  301. [ C(RESULT_MISS) ] = { CACHE_OP_UNSUPPORTED },
  302. },
  303. },
  304. [C(ITLB)] = {
  305. [C(OP_READ)] = {
  306. [C(RESULT_ACCESS)] = { CACHE_OP_UNSUPPORTED },
  307. [C(RESULT_MISS)] = { 0x04, PIC_LOWER, },
  308. },
  309. [ C(OP_WRITE) ] = {
  310. [ C(RESULT_ACCESS) ] = { CACHE_OP_UNSUPPORTED },
  311. [ C(RESULT_MISS) ] = { CACHE_OP_UNSUPPORTED },
  312. },
  313. [ C(OP_PREFETCH) ] = {
  314. [ C(RESULT_ACCESS) ] = { CACHE_OP_UNSUPPORTED },
  315. [ C(RESULT_MISS) ] = { CACHE_OP_UNSUPPORTED },
  316. },
  317. },
  318. [C(BPU)] = {
  319. [C(OP_READ)] = {
  320. [C(RESULT_ACCESS)] = { CACHE_OP_UNSUPPORTED },
  321. [C(RESULT_MISS)] = { CACHE_OP_UNSUPPORTED },
  322. },
  323. [ C(OP_WRITE) ] = {
  324. [ C(RESULT_ACCESS) ] = { CACHE_OP_UNSUPPORTED },
  325. [ C(RESULT_MISS) ] = { CACHE_OP_UNSUPPORTED },
  326. },
  327. [ C(OP_PREFETCH) ] = {
  328. [ C(RESULT_ACCESS) ] = { CACHE_OP_UNSUPPORTED },
  329. [ C(RESULT_MISS) ] = { CACHE_OP_UNSUPPORTED },
  330. },
  331. },
  332. };
  333. static const struct sparc_pmu niagara1_pmu = {
  334. .event_map = niagara1_event_map,
  335. .cache_map = &niagara1_cache_map,
  336. .max_events = ARRAY_SIZE(niagara1_perfmon_event_map),
  337. .upper_shift = 0,
  338. .lower_shift = 4,
  339. .event_mask = 0x7,
  340. .upper_nop = 0x0,
  341. .lower_nop = 0x0,
  342. };
  343. static const struct perf_event_map niagara2_perfmon_event_map[] = {
  344. [PERF_COUNT_HW_CPU_CYCLES] = { 0x02ff, PIC_UPPER | PIC_LOWER },
  345. [PERF_COUNT_HW_INSTRUCTIONS] = { 0x02ff, PIC_UPPER | PIC_LOWER },
  346. [PERF_COUNT_HW_CACHE_REFERENCES] = { 0x0208, PIC_UPPER | PIC_LOWER },
  347. [PERF_COUNT_HW_CACHE_MISSES] = { 0x0302, PIC_UPPER | PIC_LOWER },
  348. [PERF_COUNT_HW_BRANCH_INSTRUCTIONS] = { 0x0201, PIC_UPPER | PIC_LOWER },
  349. [PERF_COUNT_HW_BRANCH_MISSES] = { 0x0202, PIC_UPPER | PIC_LOWER },
  350. };
  351. static const struct perf_event_map *niagara2_event_map(int event_id)
  352. {
  353. return &niagara2_perfmon_event_map[event_id];
  354. }
  355. static const cache_map_t niagara2_cache_map = {
  356. [C(L1D)] = {
  357. [C(OP_READ)] = {
  358. [C(RESULT_ACCESS)] = { 0x0208, PIC_UPPER | PIC_LOWER, },
  359. [C(RESULT_MISS)] = { 0x0302, PIC_UPPER | PIC_LOWER, },
  360. },
  361. [C(OP_WRITE)] = {
  362. [C(RESULT_ACCESS)] = { 0x0210, PIC_UPPER | PIC_LOWER, },
  363. [C(RESULT_MISS)] = { 0x0302, PIC_UPPER | PIC_LOWER, },
  364. },
  365. [C(OP_PREFETCH)] = {
  366. [C(RESULT_ACCESS)] = { CACHE_OP_UNSUPPORTED },
  367. [C(RESULT_MISS)] = { CACHE_OP_UNSUPPORTED },
  368. },
  369. },
  370. [C(L1I)] = {
  371. [C(OP_READ)] = {
  372. [C(RESULT_ACCESS)] = { 0x02ff, PIC_UPPER | PIC_LOWER, },
  373. [C(RESULT_MISS)] = { 0x0301, PIC_UPPER | PIC_LOWER, },
  374. },
  375. [ C(OP_WRITE) ] = {
  376. [ C(RESULT_ACCESS) ] = { CACHE_OP_NONSENSE },
  377. [ C(RESULT_MISS) ] = { CACHE_OP_NONSENSE },
  378. },
  379. [ C(OP_PREFETCH) ] = {
  380. [ C(RESULT_ACCESS) ] = { CACHE_OP_UNSUPPORTED },
  381. [ C(RESULT_MISS) ] = { CACHE_OP_UNSUPPORTED },
  382. },
  383. },
  384. [C(LL)] = {
  385. [C(OP_READ)] = {
  386. [C(RESULT_ACCESS)] = { 0x0208, PIC_UPPER | PIC_LOWER, },
  387. [C(RESULT_MISS)] = { 0x0330, PIC_UPPER | PIC_LOWER, },
  388. },
  389. [C(OP_WRITE)] = {
  390. [C(RESULT_ACCESS)] = { 0x0210, PIC_UPPER | PIC_LOWER, },
  391. [C(RESULT_MISS)] = { 0x0320, PIC_UPPER | PIC_LOWER, },
  392. },
  393. [C(OP_PREFETCH)] = {
  394. [C(RESULT_ACCESS)] = { CACHE_OP_UNSUPPORTED },
  395. [C(RESULT_MISS)] = { CACHE_OP_UNSUPPORTED },
  396. },
  397. },
  398. [C(DTLB)] = {
  399. [C(OP_READ)] = {
  400. [C(RESULT_ACCESS)] = { CACHE_OP_UNSUPPORTED },
  401. [C(RESULT_MISS)] = { 0x0b08, PIC_UPPER | PIC_LOWER, },
  402. },
  403. [ C(OP_WRITE) ] = {
  404. [ C(RESULT_ACCESS) ] = { CACHE_OP_UNSUPPORTED },
  405. [ C(RESULT_MISS) ] = { CACHE_OP_UNSUPPORTED },
  406. },
  407. [ C(OP_PREFETCH) ] = {
  408. [ C(RESULT_ACCESS) ] = { CACHE_OP_UNSUPPORTED },
  409. [ C(RESULT_MISS) ] = { CACHE_OP_UNSUPPORTED },
  410. },
  411. },
  412. [C(ITLB)] = {
  413. [C(OP_READ)] = {
  414. [C(RESULT_ACCESS)] = { CACHE_OP_UNSUPPORTED },
  415. [C(RESULT_MISS)] = { 0xb04, PIC_UPPER | PIC_LOWER, },
  416. },
  417. [ C(OP_WRITE) ] = {
  418. [ C(RESULT_ACCESS) ] = { CACHE_OP_UNSUPPORTED },
  419. [ C(RESULT_MISS) ] = { CACHE_OP_UNSUPPORTED },
  420. },
  421. [ C(OP_PREFETCH) ] = {
  422. [ C(RESULT_ACCESS) ] = { CACHE_OP_UNSUPPORTED },
  423. [ C(RESULT_MISS) ] = { CACHE_OP_UNSUPPORTED },
  424. },
  425. },
  426. [C(BPU)] = {
  427. [C(OP_READ)] = {
  428. [C(RESULT_ACCESS)] = { CACHE_OP_UNSUPPORTED },
  429. [C(RESULT_MISS)] = { CACHE_OP_UNSUPPORTED },
  430. },
  431. [ C(OP_WRITE) ] = {
  432. [ C(RESULT_ACCESS) ] = { CACHE_OP_UNSUPPORTED },
  433. [ C(RESULT_MISS) ] = { CACHE_OP_UNSUPPORTED },
  434. },
  435. [ C(OP_PREFETCH) ] = {
  436. [ C(RESULT_ACCESS) ] = { CACHE_OP_UNSUPPORTED },
  437. [ C(RESULT_MISS) ] = { CACHE_OP_UNSUPPORTED },
  438. },
  439. },
  440. };
  441. static const struct sparc_pmu niagara2_pmu = {
  442. .event_map = niagara2_event_map,
  443. .cache_map = &niagara2_cache_map,
  444. .max_events = ARRAY_SIZE(niagara2_perfmon_event_map),
  445. .upper_shift = 19,
  446. .lower_shift = 6,
  447. .event_mask = 0xfff,
  448. .hv_bit = 0x8,
  449. .irq_bit = 0x30,
  450. .upper_nop = 0x220,
  451. .lower_nop = 0x220,
  452. };
  453. static const struct sparc_pmu *sparc_pmu __read_mostly;
  454. static u64 event_encoding(u64 event_id, int idx)
  455. {
  456. if (idx == PIC_UPPER_INDEX)
  457. event_id <<= sparc_pmu->upper_shift;
  458. else
  459. event_id <<= sparc_pmu->lower_shift;
  460. return event_id;
  461. }
  462. static u64 mask_for_index(int idx)
  463. {
  464. return event_encoding(sparc_pmu->event_mask, idx);
  465. }
  466. static u64 nop_for_index(int idx)
  467. {
  468. return event_encoding(idx == PIC_UPPER_INDEX ?
  469. sparc_pmu->upper_nop :
  470. sparc_pmu->lower_nop, idx);
  471. }
  472. static inline void sparc_pmu_enable_event(struct cpu_hw_events *cpuc, struct hw_perf_event *hwc, int idx)
  473. {
  474. u64 val, mask = mask_for_index(idx);
  475. val = cpuc->pcr;
  476. val &= ~mask;
  477. val |= hwc->config;
  478. cpuc->pcr = val;
  479. pcr_ops->write(cpuc->pcr);
  480. }
  481. static inline void sparc_pmu_disable_event(struct cpu_hw_events *cpuc, struct hw_perf_event *hwc, int idx)
  482. {
  483. u64 mask = mask_for_index(idx);
  484. u64 nop = nop_for_index(idx);
  485. u64 val;
  486. val = cpuc->pcr;
  487. val &= ~mask;
  488. val |= nop;
  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 u64 sparc_perf_event_update(struct perf_event *event,
  514. struct hw_perf_event *hwc, int idx)
  515. {
  516. int shift = 64 - 32;
  517. u64 prev_raw_count, new_raw_count;
  518. s64 delta;
  519. again:
  520. prev_raw_count = atomic64_read(&hwc->prev_count);
  521. new_raw_count = read_pmc(idx);
  522. if (atomic64_cmpxchg(&hwc->prev_count, prev_raw_count,
  523. new_raw_count) != prev_raw_count)
  524. goto again;
  525. delta = (new_raw_count << shift) - (prev_raw_count << shift);
  526. delta >>= shift;
  527. atomic64_add(delta, &event->count);
  528. atomic64_sub(delta, &hwc->period_left);
  529. return new_raw_count;
  530. }
  531. static int sparc_perf_event_set_period(struct perf_event *event,
  532. struct hw_perf_event *hwc, int idx)
  533. {
  534. s64 left = atomic64_read(&hwc->period_left);
  535. s64 period = hwc->sample_period;
  536. int ret = 0;
  537. if (unlikely(left <= -period)) {
  538. left = period;
  539. atomic64_set(&hwc->period_left, left);
  540. hwc->last_period = period;
  541. ret = 1;
  542. }
  543. if (unlikely(left <= 0)) {
  544. left += period;
  545. atomic64_set(&hwc->period_left, left);
  546. hwc->last_period = period;
  547. ret = 1;
  548. }
  549. if (left > MAX_PERIOD)
  550. left = MAX_PERIOD;
  551. atomic64_set(&hwc->prev_count, (u64)-left);
  552. write_pmc(idx, (u64)(-left) & 0xffffffff);
  553. perf_event_update_userpage(event);
  554. return ret;
  555. }
  556. /* If performance event entries have been added, move existing
  557. * events around (if necessary) and then assign new entries to
  558. * counters.
  559. */
  560. static u64 maybe_change_configuration(struct cpu_hw_events *cpuc, u64 pcr)
  561. {
  562. int i;
  563. if (!cpuc->n_added)
  564. goto out;
  565. /* Read in the counters which are moving. */
  566. for (i = 0; i < cpuc->n_events; i++) {
  567. struct perf_event *cp = cpuc->event[i];
  568. if (cpuc->current_idx[i] != PIC_NO_INDEX &&
  569. cpuc->current_idx[i] != cp->hw.idx) {
  570. sparc_perf_event_update(cp, &cp->hw,
  571. cpuc->current_idx[i]);
  572. cpuc->current_idx[i] = PIC_NO_INDEX;
  573. }
  574. }
  575. /* Assign to counters all unassigned events. */
  576. for (i = 0; i < cpuc->n_events; i++) {
  577. struct perf_event *cp = cpuc->event[i];
  578. struct hw_perf_event *hwc = &cp->hw;
  579. int idx = hwc->idx;
  580. u64 enc;
  581. if (cpuc->current_idx[i] != PIC_NO_INDEX)
  582. continue;
  583. sparc_perf_event_set_period(cp, hwc, idx);
  584. cpuc->current_idx[i] = idx;
  585. enc = perf_event_get_enc(cpuc->events[i]);
  586. pcr |= event_encoding(enc, idx);
  587. }
  588. out:
  589. return pcr;
  590. }
  591. void hw_perf_enable(void)
  592. {
  593. struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
  594. u64 pcr;
  595. if (cpuc->enabled)
  596. return;
  597. cpuc->enabled = 1;
  598. barrier();
  599. pcr = cpuc->pcr;
  600. if (!cpuc->n_events) {
  601. pcr = 0;
  602. } else {
  603. pcr = maybe_change_configuration(cpuc, pcr);
  604. /* We require that all of the events have the same
  605. * configuration, so just fetch the settings from the
  606. * first entry.
  607. */
  608. cpuc->pcr = pcr | cpuc->event[0]->hw.config_base;
  609. }
  610. pcr_ops->write(cpuc->pcr);
  611. }
  612. void hw_perf_disable(void)
  613. {
  614. struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
  615. u64 val;
  616. if (!cpuc->enabled)
  617. return;
  618. cpuc->enabled = 0;
  619. cpuc->n_added = 0;
  620. val = cpuc->pcr;
  621. val &= ~(PCR_UTRACE | PCR_STRACE |
  622. sparc_pmu->hv_bit | sparc_pmu->irq_bit);
  623. cpuc->pcr = val;
  624. pcr_ops->write(cpuc->pcr);
  625. }
  626. static void sparc_pmu_disable(struct perf_event *event)
  627. {
  628. struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
  629. struct hw_perf_event *hwc = &event->hw;
  630. unsigned long flags;
  631. int i;
  632. local_irq_save(flags);
  633. perf_disable();
  634. for (i = 0; i < cpuc->n_events; i++) {
  635. if (event == cpuc->event[i]) {
  636. int idx = cpuc->current_idx[i];
  637. /* Shift remaining entries down into
  638. * the existing slot.
  639. */
  640. while (++i < cpuc->n_events) {
  641. cpuc->event[i - 1] = cpuc->event[i];
  642. cpuc->events[i - 1] = cpuc->events[i];
  643. cpuc->current_idx[i - 1] =
  644. cpuc->current_idx[i];
  645. }
  646. /* Absorb the final count and turn off the
  647. * event.
  648. */
  649. sparc_pmu_disable_event(cpuc, hwc, idx);
  650. barrier();
  651. sparc_perf_event_update(event, hwc, idx);
  652. perf_event_update_userpage(event);
  653. cpuc->n_events--;
  654. break;
  655. }
  656. }
  657. perf_enable();
  658. local_irq_restore(flags);
  659. }
  660. static int active_event_index(struct cpu_hw_events *cpuc,
  661. struct perf_event *event)
  662. {
  663. int i;
  664. for (i = 0; i < cpuc->n_events; i++) {
  665. if (cpuc->event[i] == event)
  666. break;
  667. }
  668. BUG_ON(i == cpuc->n_events);
  669. return cpuc->current_idx[i];
  670. }
  671. static void sparc_pmu_read(struct perf_event *event)
  672. {
  673. struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
  674. int idx = active_event_index(cpuc, event);
  675. struct hw_perf_event *hwc = &event->hw;
  676. sparc_perf_event_update(event, hwc, idx);
  677. }
  678. static void sparc_pmu_unthrottle(struct perf_event *event)
  679. {
  680. struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
  681. int idx = active_event_index(cpuc, event);
  682. struct hw_perf_event *hwc = &event->hw;
  683. sparc_pmu_enable_event(cpuc, hwc, idx);
  684. }
  685. static atomic_t active_events = ATOMIC_INIT(0);
  686. static DEFINE_MUTEX(pmc_grab_mutex);
  687. static void perf_stop_nmi_watchdog(void *unused)
  688. {
  689. struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
  690. stop_nmi_watchdog(NULL);
  691. cpuc->pcr = pcr_ops->read();
  692. }
  693. void perf_event_grab_pmc(void)
  694. {
  695. if (atomic_inc_not_zero(&active_events))
  696. return;
  697. mutex_lock(&pmc_grab_mutex);
  698. if (atomic_read(&active_events) == 0) {
  699. if (atomic_read(&nmi_active) > 0) {
  700. on_each_cpu(perf_stop_nmi_watchdog, NULL, 1);
  701. BUG_ON(atomic_read(&nmi_active) != 0);
  702. }
  703. atomic_inc(&active_events);
  704. }
  705. mutex_unlock(&pmc_grab_mutex);
  706. }
  707. void perf_event_release_pmc(void)
  708. {
  709. if (atomic_dec_and_mutex_lock(&active_events, &pmc_grab_mutex)) {
  710. if (atomic_read(&nmi_active) == 0)
  711. on_each_cpu(start_nmi_watchdog, NULL, 1);
  712. mutex_unlock(&pmc_grab_mutex);
  713. }
  714. }
  715. static const struct perf_event_map *sparc_map_cache_event(u64 config)
  716. {
  717. unsigned int cache_type, cache_op, cache_result;
  718. const struct perf_event_map *pmap;
  719. if (!sparc_pmu->cache_map)
  720. return ERR_PTR(-ENOENT);
  721. cache_type = (config >> 0) & 0xff;
  722. if (cache_type >= PERF_COUNT_HW_CACHE_MAX)
  723. return ERR_PTR(-EINVAL);
  724. cache_op = (config >> 8) & 0xff;
  725. if (cache_op >= PERF_COUNT_HW_CACHE_OP_MAX)
  726. return ERR_PTR(-EINVAL);
  727. cache_result = (config >> 16) & 0xff;
  728. if (cache_result >= PERF_COUNT_HW_CACHE_RESULT_MAX)
  729. return ERR_PTR(-EINVAL);
  730. pmap = &((*sparc_pmu->cache_map)[cache_type][cache_op][cache_result]);
  731. if (pmap->encoding == CACHE_OP_UNSUPPORTED)
  732. return ERR_PTR(-ENOENT);
  733. if (pmap->encoding == CACHE_OP_NONSENSE)
  734. return ERR_PTR(-EINVAL);
  735. return pmap;
  736. }
  737. static void hw_perf_event_destroy(struct perf_event *event)
  738. {
  739. perf_event_release_pmc();
  740. }
  741. /* Make sure all events can be scheduled into the hardware at
  742. * the same time. This is simplified by the fact that we only
  743. * need to support 2 simultaneous HW events.
  744. *
  745. * As a side effect, the evts[]->hw.idx values will be assigned
  746. * on success. These are pending indexes. When the events are
  747. * actually programmed into the chip, these values will propagate
  748. * to the per-cpu cpuc->current_idx[] slots, see the code in
  749. * maybe_change_configuration() for details.
  750. */
  751. static int sparc_check_constraints(struct perf_event **evts,
  752. unsigned long *events, int n_ev)
  753. {
  754. u8 msk0 = 0, msk1 = 0;
  755. int idx0 = 0;
  756. /* This case is possible when we are invoked from
  757. * hw_perf_group_sched_in().
  758. */
  759. if (!n_ev)
  760. return 0;
  761. if (n_ev > perf_max_events)
  762. return -1;
  763. msk0 = perf_event_get_msk(events[0]);
  764. if (n_ev == 1) {
  765. if (msk0 & PIC_LOWER)
  766. idx0 = 1;
  767. goto success;
  768. }
  769. BUG_ON(n_ev != 2);
  770. msk1 = perf_event_get_msk(events[1]);
  771. /* If both events can go on any counter, OK. */
  772. if (msk0 == (PIC_UPPER | PIC_LOWER) &&
  773. msk1 == (PIC_UPPER | PIC_LOWER))
  774. goto success;
  775. /* If one event is limited to a specific counter,
  776. * and the other can go on both, OK.
  777. */
  778. if ((msk0 == PIC_UPPER || msk0 == PIC_LOWER) &&
  779. msk1 == (PIC_UPPER | PIC_LOWER)) {
  780. if (msk0 & PIC_LOWER)
  781. idx0 = 1;
  782. goto success;
  783. }
  784. if ((msk1 == PIC_UPPER || msk1 == PIC_LOWER) &&
  785. msk0 == (PIC_UPPER | PIC_LOWER)) {
  786. if (msk1 & PIC_UPPER)
  787. idx0 = 1;
  788. goto success;
  789. }
  790. /* If the events are fixed to different counters, OK. */
  791. if ((msk0 == PIC_UPPER && msk1 == PIC_LOWER) ||
  792. (msk0 == PIC_LOWER && msk1 == PIC_UPPER)) {
  793. if (msk0 & PIC_LOWER)
  794. idx0 = 1;
  795. goto success;
  796. }
  797. /* Otherwise, there is a conflict. */
  798. return -1;
  799. success:
  800. evts[0]->hw.idx = idx0;
  801. if (n_ev == 2)
  802. evts[1]->hw.idx = idx0 ^ 1;
  803. return 0;
  804. }
  805. static int check_excludes(struct perf_event **evts, int n_prev, int n_new)
  806. {
  807. int eu = 0, ek = 0, eh = 0;
  808. struct perf_event *event;
  809. int i, n, first;
  810. n = n_prev + n_new;
  811. if (n <= 1)
  812. return 0;
  813. first = 1;
  814. for (i = 0; i < n; i++) {
  815. event = evts[i];
  816. if (first) {
  817. eu = event->attr.exclude_user;
  818. ek = event->attr.exclude_kernel;
  819. eh = event->attr.exclude_hv;
  820. first = 0;
  821. } else if (event->attr.exclude_user != eu ||
  822. event->attr.exclude_kernel != ek ||
  823. event->attr.exclude_hv != eh) {
  824. return -EAGAIN;
  825. }
  826. }
  827. return 0;
  828. }
  829. static int collect_events(struct perf_event *group, int max_count,
  830. struct perf_event *evts[], unsigned long *events,
  831. int *current_idx)
  832. {
  833. struct perf_event *event;
  834. int n = 0;
  835. if (!is_software_event(group)) {
  836. if (n >= max_count)
  837. return -1;
  838. evts[n] = group;
  839. events[n] = group->hw.event_base;
  840. current_idx[n++] = PIC_NO_INDEX;
  841. }
  842. list_for_each_entry(event, &group->sibling_list, group_entry) {
  843. if (!is_software_event(event) &&
  844. event->state != PERF_EVENT_STATE_OFF) {
  845. if (n >= max_count)
  846. return -1;
  847. evts[n] = event;
  848. events[n] = event->hw.event_base;
  849. current_idx[n++] = PIC_NO_INDEX;
  850. }
  851. }
  852. return n;
  853. }
  854. static void event_sched_in(struct perf_event *event)
  855. {
  856. event->state = PERF_EVENT_STATE_ACTIVE;
  857. event->oncpu = smp_processor_id();
  858. event->tstamp_running += event->ctx->time - event->tstamp_stopped;
  859. if (is_software_event(event))
  860. event->pmu->enable(event);
  861. }
  862. int hw_perf_group_sched_in(struct perf_event *group_leader,
  863. struct perf_cpu_context *cpuctx,
  864. struct perf_event_context *ctx)
  865. {
  866. struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
  867. struct perf_event *sub;
  868. int n0, n;
  869. if (!sparc_pmu)
  870. return 0;
  871. n0 = cpuc->n_events;
  872. n = collect_events(group_leader, perf_max_events - n0,
  873. &cpuc->event[n0], &cpuc->events[n0],
  874. &cpuc->current_idx[n0]);
  875. if (n < 0)
  876. return -EAGAIN;
  877. if (check_excludes(cpuc->event, n0, n))
  878. return -EINVAL;
  879. if (sparc_check_constraints(cpuc->event, cpuc->events, n + n0))
  880. return -EAGAIN;
  881. cpuc->n_events = n0 + n;
  882. cpuc->n_added += n;
  883. cpuctx->active_oncpu += n;
  884. n = 1;
  885. event_sched_in(group_leader);
  886. list_for_each_entry(sub, &group_leader->sibling_list, group_entry) {
  887. if (sub->state != PERF_EVENT_STATE_OFF) {
  888. event_sched_in(sub);
  889. n++;
  890. }
  891. }
  892. ctx->nr_active += n;
  893. return 1;
  894. }
  895. static int sparc_pmu_enable(struct perf_event *event)
  896. {
  897. struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
  898. int n0, ret = -EAGAIN;
  899. unsigned long flags;
  900. local_irq_save(flags);
  901. perf_disable();
  902. n0 = cpuc->n_events;
  903. if (n0 >= perf_max_events)
  904. goto out;
  905. cpuc->event[n0] = event;
  906. cpuc->events[n0] = event->hw.event_base;
  907. cpuc->current_idx[n0] = PIC_NO_INDEX;
  908. if (check_excludes(cpuc->event, n0, 1))
  909. goto out;
  910. if (sparc_check_constraints(cpuc->event, cpuc->events, n0 + 1))
  911. goto out;
  912. cpuc->n_events++;
  913. cpuc->n_added++;
  914. ret = 0;
  915. out:
  916. perf_enable();
  917. local_irq_restore(flags);
  918. return ret;
  919. }
  920. static int __hw_perf_event_init(struct perf_event *event)
  921. {
  922. struct perf_event_attr *attr = &event->attr;
  923. struct perf_event *evts[MAX_HWEVENTS];
  924. struct hw_perf_event *hwc = &event->hw;
  925. unsigned long events[MAX_HWEVENTS];
  926. int current_idx_dmy[MAX_HWEVENTS];
  927. const struct perf_event_map *pmap;
  928. int n;
  929. if (atomic_read(&nmi_active) < 0)
  930. return -ENODEV;
  931. if (attr->type == PERF_TYPE_HARDWARE) {
  932. if (attr->config >= sparc_pmu->max_events)
  933. return -EINVAL;
  934. pmap = sparc_pmu->event_map(attr->config);
  935. } else if (attr->type == PERF_TYPE_HW_CACHE) {
  936. pmap = sparc_map_cache_event(attr->config);
  937. if (IS_ERR(pmap))
  938. return PTR_ERR(pmap);
  939. } else
  940. return -EOPNOTSUPP;
  941. /* We save the enable bits in the config_base. */
  942. hwc->config_base = sparc_pmu->irq_bit;
  943. if (!attr->exclude_user)
  944. hwc->config_base |= PCR_UTRACE;
  945. if (!attr->exclude_kernel)
  946. hwc->config_base |= PCR_STRACE;
  947. if (!attr->exclude_hv)
  948. hwc->config_base |= sparc_pmu->hv_bit;
  949. hwc->event_base = perf_event_encode(pmap);
  950. n = 0;
  951. if (event->group_leader != event) {
  952. n = collect_events(event->group_leader,
  953. perf_max_events - 1,
  954. evts, events, current_idx_dmy);
  955. if (n < 0)
  956. return -EINVAL;
  957. }
  958. events[n] = hwc->event_base;
  959. evts[n] = event;
  960. if (check_excludes(evts, n, 1))
  961. return -EINVAL;
  962. if (sparc_check_constraints(evts, events, n + 1))
  963. return -EINVAL;
  964. hwc->idx = PIC_NO_INDEX;
  965. /* Try to do all error checking before this point, as unwinding
  966. * state after grabbing the PMC is difficult.
  967. */
  968. perf_event_grab_pmc();
  969. event->destroy = hw_perf_event_destroy;
  970. if (!hwc->sample_period) {
  971. hwc->sample_period = MAX_PERIOD;
  972. hwc->last_period = hwc->sample_period;
  973. atomic64_set(&hwc->period_left, hwc->sample_period);
  974. }
  975. return 0;
  976. }
  977. static const struct pmu pmu = {
  978. .enable = sparc_pmu_enable,
  979. .disable = sparc_pmu_disable,
  980. .read = sparc_pmu_read,
  981. .unthrottle = sparc_pmu_unthrottle,
  982. };
  983. const struct pmu *hw_perf_event_init(struct perf_event *event)
  984. {
  985. int err = __hw_perf_event_init(event);
  986. if (err)
  987. return ERR_PTR(err);
  988. return &pmu;
  989. }
  990. void perf_event_print_debug(void)
  991. {
  992. unsigned long flags;
  993. u64 pcr, pic;
  994. int cpu;
  995. if (!sparc_pmu)
  996. return;
  997. local_irq_save(flags);
  998. cpu = smp_processor_id();
  999. pcr = pcr_ops->read();
  1000. read_pic(pic);
  1001. pr_info("\n");
  1002. pr_info("CPU#%d: PCR[%016llx] PIC[%016llx]\n",
  1003. cpu, pcr, pic);
  1004. local_irq_restore(flags);
  1005. }
  1006. static int __kprobes perf_event_nmi_handler(struct notifier_block *self,
  1007. unsigned long cmd, void *__args)
  1008. {
  1009. struct die_args *args = __args;
  1010. struct perf_sample_data data;
  1011. struct cpu_hw_events *cpuc;
  1012. struct pt_regs *regs;
  1013. int i;
  1014. if (!atomic_read(&active_events))
  1015. return NOTIFY_DONE;
  1016. switch (cmd) {
  1017. case DIE_NMI:
  1018. break;
  1019. default:
  1020. return NOTIFY_DONE;
  1021. }
  1022. regs = args->regs;
  1023. perf_sample_data_init(&data, 0);
  1024. cpuc = &__get_cpu_var(cpu_hw_events);
  1025. /* If the PMU has the TOE IRQ enable bits, we need to do a
  1026. * dummy write to the %pcr to clear the overflow bits and thus
  1027. * the interrupt.
  1028. *
  1029. * Do this before we peek at the counters to determine
  1030. * overflow so we don't lose any events.
  1031. */
  1032. if (sparc_pmu->irq_bit)
  1033. pcr_ops->write(cpuc->pcr);
  1034. for (i = 0; i < cpuc->n_events; i++) {
  1035. struct perf_event *event = cpuc->event[i];
  1036. int idx = cpuc->current_idx[i];
  1037. struct hw_perf_event *hwc;
  1038. u64 val;
  1039. hwc = &event->hw;
  1040. val = sparc_perf_event_update(event, hwc, idx);
  1041. if (val & (1ULL << 31))
  1042. continue;
  1043. data.period = event->hw.last_period;
  1044. if (!sparc_perf_event_set_period(event, hwc, idx))
  1045. continue;
  1046. if (perf_event_overflow(event, 1, &data, regs))
  1047. sparc_pmu_disable_event(cpuc, hwc, idx);
  1048. }
  1049. return NOTIFY_STOP;
  1050. }
  1051. static __read_mostly struct notifier_block perf_event_nmi_notifier = {
  1052. .notifier_call = perf_event_nmi_handler,
  1053. };
  1054. static bool __init supported_pmu(void)
  1055. {
  1056. if (!strcmp(sparc_pmu_type, "ultra3") ||
  1057. !strcmp(sparc_pmu_type, "ultra3+") ||
  1058. !strcmp(sparc_pmu_type, "ultra3i") ||
  1059. !strcmp(sparc_pmu_type, "ultra4+")) {
  1060. sparc_pmu = &ultra3_pmu;
  1061. return true;
  1062. }
  1063. if (!strcmp(sparc_pmu_type, "niagara")) {
  1064. sparc_pmu = &niagara1_pmu;
  1065. return true;
  1066. }
  1067. if (!strcmp(sparc_pmu_type, "niagara2")) {
  1068. sparc_pmu = &niagara2_pmu;
  1069. return true;
  1070. }
  1071. return false;
  1072. }
  1073. void __init init_hw_perf_events(void)
  1074. {
  1075. pr_info("Performance events: ");
  1076. if (!supported_pmu()) {
  1077. pr_cont("No support for PMU type '%s'\n", sparc_pmu_type);
  1078. return;
  1079. }
  1080. pr_cont("Supported PMU type is '%s'\n", sparc_pmu_type);
  1081. /* All sparc64 PMUs currently have 2 events. */
  1082. perf_max_events = 2;
  1083. register_die_notifier(&perf_event_nmi_notifier);
  1084. }
  1085. static inline void callchain_store(struct perf_callchain_entry *entry, u64 ip)
  1086. {
  1087. if (entry->nr < PERF_MAX_STACK_DEPTH)
  1088. entry->ip[entry->nr++] = ip;
  1089. }
  1090. static void perf_callchain_kernel(struct pt_regs *regs,
  1091. struct perf_callchain_entry *entry)
  1092. {
  1093. unsigned long ksp, fp;
  1094. callchain_store(entry, PERF_CONTEXT_KERNEL);
  1095. callchain_store(entry, regs->tpc);
  1096. ksp = regs->u_regs[UREG_I6];
  1097. fp = ksp + STACK_BIAS;
  1098. do {
  1099. struct sparc_stackf *sf;
  1100. struct pt_regs *regs;
  1101. unsigned long pc;
  1102. if (!kstack_valid(current_thread_info(), fp))
  1103. break;
  1104. sf = (struct sparc_stackf *) fp;
  1105. regs = (struct pt_regs *) (sf + 1);
  1106. if (kstack_is_trap_frame(current_thread_info(), regs)) {
  1107. if (user_mode(regs))
  1108. break;
  1109. pc = regs->tpc;
  1110. fp = regs->u_regs[UREG_I6] + STACK_BIAS;
  1111. } else {
  1112. pc = sf->callers_pc;
  1113. fp = (unsigned long)sf->fp + STACK_BIAS;
  1114. }
  1115. callchain_store(entry, pc);
  1116. } while (entry->nr < PERF_MAX_STACK_DEPTH);
  1117. }
  1118. static void perf_callchain_user_64(struct pt_regs *regs,
  1119. struct perf_callchain_entry *entry)
  1120. {
  1121. unsigned long ufp;
  1122. callchain_store(entry, PERF_CONTEXT_USER);
  1123. callchain_store(entry, regs->tpc);
  1124. ufp = regs->u_regs[UREG_I6] + STACK_BIAS;
  1125. do {
  1126. struct sparc_stackf *usf, sf;
  1127. unsigned long pc;
  1128. usf = (struct sparc_stackf *) ufp;
  1129. if (__copy_from_user_inatomic(&sf, usf, sizeof(sf)))
  1130. break;
  1131. pc = sf.callers_pc;
  1132. ufp = (unsigned long)sf.fp + STACK_BIAS;
  1133. callchain_store(entry, pc);
  1134. } while (entry->nr < PERF_MAX_STACK_DEPTH);
  1135. }
  1136. static void perf_callchain_user_32(struct pt_regs *regs,
  1137. struct perf_callchain_entry *entry)
  1138. {
  1139. unsigned long ufp;
  1140. callchain_store(entry, PERF_CONTEXT_USER);
  1141. callchain_store(entry, regs->tpc);
  1142. ufp = regs->u_regs[UREG_I6] & 0xffffffffUL;
  1143. do {
  1144. struct sparc_stackf32 *usf, sf;
  1145. unsigned long pc;
  1146. usf = (struct sparc_stackf32 *) ufp;
  1147. if (__copy_from_user_inatomic(&sf, usf, sizeof(sf)))
  1148. break;
  1149. pc = sf.callers_pc;
  1150. ufp = (unsigned long)sf.fp;
  1151. callchain_store(entry, pc);
  1152. } while (entry->nr < PERF_MAX_STACK_DEPTH);
  1153. }
  1154. /* Like powerpc we can't get PMU interrupts within the PMU handler,
  1155. * so no need for separate NMI and IRQ chains as on x86.
  1156. */
  1157. static DEFINE_PER_CPU(struct perf_callchain_entry, callchain);
  1158. struct perf_callchain_entry *perf_callchain(struct pt_regs *regs)
  1159. {
  1160. struct perf_callchain_entry *entry = &__get_cpu_var(callchain);
  1161. entry->nr = 0;
  1162. if (!user_mode(regs)) {
  1163. stack_trace_flush();
  1164. perf_callchain_kernel(regs, entry);
  1165. if (current->mm)
  1166. regs = task_pt_regs(current);
  1167. else
  1168. regs = NULL;
  1169. }
  1170. if (regs) {
  1171. flushw_user();
  1172. if (test_thread_flag(TIF_32BIT))
  1173. perf_callchain_user_32(regs, entry);
  1174. else
  1175. perf_callchain_user_64(regs, entry);
  1176. }
  1177. return entry;
  1178. }