perf_event.c 34 KB

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