perf_event.c 34 KB

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