perf_event.c 36 KB

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